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;
}
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);
}
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));
}
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));
}
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);
}
Aggregations