Search in sources :

Example 11 with MethodArgumentNotValidException

use of org.springframework.web.bind.MethodArgumentNotValidException in project chassis by Kixeye.

the class ExceptionServiceErrorMapper method mapException.

/**
 * Maps an exception to an error.
 *
 * @param ex
 * @return
 */
public static ServiceError mapException(Throwable ex) {
    ServiceError error = null;
    if (ex instanceof ServiceException) {
        ServiceException servEx = (ServiceException) ex;
        error = servEx.error;
    } else if (ex instanceof MethodArgumentNotValidException) {
        MethodArgumentNotValidException validationEx = (MethodArgumentNotValidException) ex;
        List<String> errors = Lists.newArrayList();
        for (ObjectError objError : validationEx.getBindingResult().getAllErrors()) {
            errors.add(objError.getObjectName() + ":" + objError.getCode() + ":" + objError.getDefaultMessage());
        }
        error = new ServiceError(VALIDATION_ERROR_CODE, Joiner.on("|").join(errors));
    } else {
        error = new ServiceError(UNKNOWN_ERROR_CODE, ex.getMessage());
    }
    return error;
}
Also used : ServiceError(com.kixeye.chassis.transport.dto.ServiceError) ObjectError(org.springframework.validation.ObjectError) List(java.util.List) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException)

Example 12 with MethodArgumentNotValidException

use of org.springframework.web.bind.MethodArgumentNotValidException in project spring-boot by spring-projects.

the class DefaultErrorAttributesTests method extractMethodArgumentNotValidExceptionBindingResultErrors.

@Test
public void extractMethodArgumentNotValidExceptionBindingResultErrors() throws Exception {
    BindingResult bindingResult = new MapBindingResult(Collections.singletonMap("a", "b"), "objectName");
    bindingResult.addError(new ObjectError("c", "d"));
    Exception ex = new MethodArgumentNotValidException(null, bindingResult);
    testBindingResult(bindingResult, ex);
}
Also used : BindingResult(org.springframework.validation.BindingResult) MapBindingResult(org.springframework.validation.MapBindingResult) ObjectError(org.springframework.validation.ObjectError) MapBindingResult(org.springframework.validation.MapBindingResult) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) ServletException(javax.servlet.ServletException) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) BindException(org.springframework.validation.BindException) Test(org.junit.Test)

Example 13 with MethodArgumentNotValidException

use of org.springframework.web.bind.MethodArgumentNotValidException in project rpki-validator-3 by RIPE-NCC.

the class ApiErrorHandler method handleMethodArgumentNotValid.

@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
    Locale locale = request.getLocale();
    List<ApiError> errors = ex.getBindingResult().getFieldErrors().stream().map((fieldError) -> ApiError.builder().status(String.valueOf(HttpStatus.BAD_REQUEST.value())).code(fieldError.getCode()).title(messages.getMessage("title." + fieldError.getCode(), null, HttpStatus.BAD_REQUEST.getReasonPhrase(), locale)).detail(messages.getMessage(fieldError, locale)).source(ApiErrorSource.of(Optional.of("/" + fieldError.getField().replaceAll("[.\\[]", "/").replace("]", "")), Optional.empty())).build()).collect(Collectors.toList());
    return ResponseEntity.badRequest().contentType(MediaType.valueOf(Api.API_MIME_TYPE)).body(ApiResponse.error(errors));
}
Also used : Locale(java.util.Locale) ControllerAdvice(org.springframework.web.bind.annotation.ControllerAdvice) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) WebRequest(org.springframework.web.context.request.WebRequest) Autowired(org.springframework.beans.factory.annotation.Autowired) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) DataRetrievalFailureException(org.springframework.dao.DataRetrievalFailureException) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) MethodArgumentTypeMismatchException(org.springframework.web.method.annotation.MethodArgumentTypeMismatchException) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) Locale(java.util.Locale) ResponseEntityExceptionHandler(org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) Optional(java.util.Optional) ResponseEntity(org.springframework.http.ResponseEntity) MessageSource(org.springframework.context.MessageSource)

Example 14 with MethodArgumentNotValidException

use of org.springframework.web.bind.MethodArgumentNotValidException in project rpki-validator-3 by RIPE-NCC.

the class ApiErrorHandler method handleMethodArgumentNotValid.

@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
    Locale locale = request.getLocale();
    List<ApiError> errors = ex.getBindingResult().getFieldErrors().stream().map((fieldError) -> ApiError.builder().status(String.valueOf(HttpStatus.BAD_REQUEST.value())).code(fieldError.getCode()).title(messages.getMessage("title." + fieldError.getCode(), null, HttpStatus.BAD_REQUEST.getReasonPhrase(), locale)).detail(messages.getMessage(fieldError, locale)).source(ApiErrorSource.of(Optional.of("/" + fieldError.getField().replaceAll("[.\\[]", "/").replace("]", "")), Optional.empty())).build()).collect(Collectors.toList());
    return ResponseEntity.badRequest().contentType(MediaType.valueOf(Api.API_MIME_TYPE)).body(ApiResponse.error(errors));
}
Also used : Locale(java.util.Locale) ControllerAdvice(org.springframework.web.bind.annotation.ControllerAdvice) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) WebRequest(org.springframework.web.context.request.WebRequest) Autowired(org.springframework.beans.factory.annotation.Autowired) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) DataRetrievalFailureException(org.springframework.dao.DataRetrievalFailureException) RestController(org.springframework.web.bind.annotation.RestController) Collectors(java.util.stream.Collectors) MethodArgumentTypeMismatchException(org.springframework.web.method.annotation.MethodArgumentTypeMismatchException) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) Locale(java.util.Locale) ResponseEntityExceptionHandler(org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) Optional(java.util.Optional) ResponseEntity(org.springframework.http.ResponseEntity) MessageSource(org.springframework.context.MessageSource)

Example 15 with MethodArgumentNotValidException

use of org.springframework.web.bind.MethodArgumentNotValidException 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)

Aggregations

MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)27 List (java.util.List)18 BindingResult (org.springframework.validation.BindingResult)17 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)17 ResponseEntity (org.springframework.http.ResponseEntity)16 ControllerAdvice (org.springframework.web.bind.annotation.ControllerAdvice)16 Collectors (java.util.stream.Collectors)15 Nonnull (javax.annotation.Nonnull)13 Nullable (javax.annotation.Nullable)13 HttpServletRequest (javax.servlet.http.HttpServletRequest)13 NativeWebRequest (org.springframework.web.context.request.NativeWebRequest)13 DefaultProblem (org.zalando.problem.DefaultProblem)13 Problem (org.zalando.problem.Problem)13 ProblemBuilder (org.zalando.problem.ProblemBuilder)13 Status (org.zalando.problem.Status)13 ProblemHandling (org.zalando.problem.spring.web.advice.ProblemHandling)13 ConstraintViolationProblem (org.zalando.problem.spring.web.advice.validation.ConstraintViolationProblem)13 ConcurrencyFailureException (org.springframework.dao.ConcurrencyFailureException)12 HeaderUtil (io.github.jhipster.sample.web.rest.util.HeaderUtil)7 NoSuchElementException (java.util.NoSuchElementException)7