Search in sources :

Example 31 with Problem

use of org.zalando.problem.Problem in project jhipster-sample-app-elasticsearch by jhipster.

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)

Example 32 with Problem

use of org.zalando.problem.Problem 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)

Example 33 with Problem

use of org.zalando.problem.Problem in project jhipster-sample-app-mongodb by jhipster.

the class ExceptionTranslator method handleMethodArgumentNotValid.

@Override
public ResponseEntity<Problem> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, @Nonnull NativeWebRequest request) {
    BindingResult result = ex.getBindingResult();
    List<FieldErrorVM> fieldErrors = result.getFieldErrors().stream().map(f -> new FieldErrorVM(f.getObjectName(), f.getField(), f.getCode())).collect(Collectors.toList());
    Problem problem = Problem.builder().withType(ErrorConstants.CONSTRAINT_VIOLATION_TYPE).withTitle("Method argument not valid").withStatus(defaultConstraintViolationStatus()).with("message", ErrorConstants.ERR_VALIDATION).with("fieldErrors", fieldErrors).build();
    return create(ex, problem, request);
}
Also used : ControllerAdvice(org.springframework.web.bind.annotation.ControllerAdvice) ConcurrencyFailureException(org.springframework.dao.ConcurrencyFailureException) BindingResult(org.springframework.validation.BindingResult) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) ProblemHandling(org.zalando.problem.spring.web.advice.ProblemHandling) Collectors(java.util.stream.Collectors) DefaultProblem(org.zalando.problem.DefaultProblem) NativeWebRequest(org.springframework.web.context.request.NativeWebRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) List(java.util.List) HeaderUtil(io.github.jhipster.sample.web.rest.util.HeaderUtil) Problem(org.zalando.problem.Problem) ProblemBuilder(org.zalando.problem.ProblemBuilder) Status(org.zalando.problem.Status) ConstraintViolationProblem(org.zalando.problem.spring.web.advice.validation.ConstraintViolationProblem) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseEntity(org.springframework.http.ResponseEntity) NoSuchElementException(java.util.NoSuchElementException) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) BindingResult(org.springframework.validation.BindingResult) DefaultProblem(org.zalando.problem.DefaultProblem) Problem(org.zalando.problem.Problem) ConstraintViolationProblem(org.zalando.problem.spring.web.advice.validation.ConstraintViolationProblem)

Example 34 with Problem

use of org.zalando.problem.Problem in project jhipster-sample-app-mongodb by jhipster.

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)

Example 35 with Problem

use of org.zalando.problem.Problem in project jhipster-sample-app-hazelcast by jhipster.

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

Problem (org.zalando.problem.Problem)75 Test (org.junit.Test)47 TestUtils.invalidProblem (org.zalando.nakadi.utils.TestUtils.invalidProblem)32 ResponseEntity (org.springframework.http.ResponseEntity)26 DefaultProblem (org.zalando.problem.DefaultProblem)26 ProblemBuilder (org.zalando.problem.ProblemBuilder)26 ConstraintViolationProblem (org.zalando.problem.spring.web.advice.validation.ConstraintViolationProblem)26 ThrowableProblem (org.zalando.problem.ThrowableProblem)25 EventType (org.zalando.nakadi.domain.EventType)18 TestUtils.buildDefaultEventType (org.zalando.nakadi.utils.TestUtils.buildDefaultEventType)18 NativeWebRequest (org.springframework.web.context.request.NativeWebRequest)14 List (java.util.List)13 Collectors (java.util.stream.Collectors)13 Nonnull (javax.annotation.Nonnull)13 Nullable (javax.annotation.Nullable)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)13 BindingResult (org.springframework.validation.BindingResult)13 MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)13 ControllerAdvice (org.springframework.web.bind.annotation.ControllerAdvice)13 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)13