Search in sources :

Example 1 with ProblemBuilder

use of org.zalando.problem.ProblemBuilder in project uplace.es by Uplace.

the class ExceptionTranslator method process.

/**
 * Post-process Problem payload to add the message key for front-end if needed
 */
@Override
public ResponseEntity<Problem> process(@Nullable ResponseEntity<Problem> entity, NativeWebRequest request) {
    if (entity == null || entity.getBody() == null) {
        return entity;
    }
    Problem problem = entity.getBody();
    if (!(problem instanceof ConstraintViolationProblem || problem instanceof DefaultProblem)) {
        return entity;
    }
    ProblemBuilder builder = Problem.builder().withType(Problem.DEFAULT_TYPE.equals(problem.getType()) ? ErrorConstants.DEFAULT_TYPE : problem.getType()).withStatus(problem.getStatus()).withTitle(problem.getTitle()).with("path", request.getNativeRequest(HttpServletRequest.class).getRequestURI());
    if (problem instanceof ConstraintViolationProblem) {
        builder.with("violations", ((ConstraintViolationProblem) problem).getViolations()).with("message", ErrorConstants.ERR_VALIDATION);
        return new ResponseEntity<>(builder.build(), entity.getHeaders(), entity.getStatusCode());
    } else {
        builder.withCause(((DefaultProblem) problem).getCause()).withDetail(problem.getDetail()).withInstance(problem.getInstance());
        problem.getParameters().forEach(builder::with);
        if (!problem.getParameters().containsKey("message") && problem.getStatus() != null) {
            builder.with("message", "error.http." + problem.getStatus().getStatusCode());
        }
        return new ResponseEntity<>(builder.build(), entity.getHeaders(), entity.getStatusCode());
    }
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) DefaultProblem(org.zalando.problem.DefaultProblem) DefaultProblem(org.zalando.problem.DefaultProblem) Problem(org.zalando.problem.Problem) ConstraintViolationProblem(org.zalando.problem.spring.web.advice.validation.ConstraintViolationProblem) ConstraintViolationProblem(org.zalando.problem.spring.web.advice.validation.ConstraintViolationProblem) ProblemBuilder(org.zalando.problem.ProblemBuilder)

Aggregations

ResponseEntity (org.springframework.http.ResponseEntity)1 DefaultProblem (org.zalando.problem.DefaultProblem)1 Problem (org.zalando.problem.Problem)1 ProblemBuilder (org.zalando.problem.ProblemBuilder)1 ConstraintViolationProblem (org.zalando.problem.spring.web.advice.validation.ConstraintViolationProblem)1