use of org.springframework.validation.FieldError in project spring-cloud-open-service-broker by spring-cloud.
the class BaseControllerTest method methodArgumentNotValidExceptionGivesExpectedStatus.
@Test
public void methodArgumentNotValidExceptionGivesExpectedStatus() throws NoSuchMethodException {
BindingResult bindingResult = new MapBindingResult(new HashMap<>(), "objectName");
bindingResult.addError(new FieldError("objectName", "field1", "message"));
bindingResult.addError(new FieldError("objectName", "field2", "message"));
Method method = this.getClass().getMethod("setUp", (Class<?>[]) null);
MethodParameter parameter = new MethodParameter(method, -1);
MethodArgumentNotValidException exception = new MethodArgumentNotValidException(parameter, bindingResult);
ResponseEntity<ErrorMessage> responseEntity = controller.handleException(exception);
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.UNPROCESSABLE_ENTITY);
assertThat(responseEntity.getBody().getError()).isNull();
assertThat(responseEntity.getBody().getMessage()).contains("field1");
assertThat(responseEntity.getBody().getMessage()).contains("field2");
}
use of org.springframework.validation.FieldError in project spring-cloud-open-service-broker by spring-cloud.
the class BaseControllerTest method webExchangeBindExceptionGivesExpectedStatus.
@Test
public void webExchangeBindExceptionGivesExpectedStatus() throws NoSuchMethodException {
BindingResult bindingResult = new MapBindingResult(new HashMap<>(), "objectName");
bindingResult.addError(new FieldError("objectName", "field1", "message"));
bindingResult.addError(new FieldError("objectName", "field2", "message"));
Method method = this.getClass().getMethod("setUp", (Class<?>[]) null);
MethodParameter parameter = new MethodParameter(method, -1);
WebExchangeBindException exception = new WebExchangeBindException(parameter, bindingResult);
ResponseEntity<ErrorMessage> responseEntity = controller.handleException(exception);
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.UNPROCESSABLE_ENTITY);
assertThat(responseEntity.getBody().getError()).isNull();
assertThat(responseEntity.getBody().getMessage()).contains("field1");
assertThat(responseEntity.getBody().getMessage()).contains("field2");
}
use of org.springframework.validation.FieldError in project entando-core by entando.
the class RestExceptionHandler method processFieldErrors.
private RestResponse processFieldErrors(RestResponse dto, List<FieldError> fieldErrors) {
if (null != fieldErrors) {
List<RestError> errors = new ArrayList<>();
for (FieldError fieldError : fieldErrors) {
String localizedErrorMessage = resolveLocalizedErrorMessage(fieldError);
errors.add(new RestError(fieldError.getCode(), localizedErrorMessage));
}
dto.addErrors(errors);
}
return dto;
}
use of org.springframework.validation.FieldError in project flytecnologia-api by jullierme.
the class FlyExceptionHandler method handleNotAuthenticated.
@ExceptionHandler(value = { ConstraintViolationException.class })
protected ResponseEntity<Object> handleNotAuthenticated(RuntimeException ex, WebRequest request) {
String fieldError = ((ConstraintViolationException) ex.getCause()).getConstraintName();
List<Error> errors = getListOfErros(fieldError, ex);
return handleExceptionInternal(ex, errors, new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
}
use of org.springframework.validation.FieldError in project tutorials by eugenp.
the class ExceptionTranslator method processValidationError.
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ErrorVM processValidationError(MethodArgumentNotValidException ex) {
BindingResult result = ex.getBindingResult();
List<FieldError> fieldErrors = result.getFieldErrors();
return processFieldErrors(fieldErrors);
}
Aggregations