use of org.springframework.validation.FieldError in project flytecnologia-api by jullierme.
the class FlyExceptionHandler method createListOfErros.
private List<Error> createListOfErros(BindingResult bindingResult) {
List<Error> erros = new ArrayList<>();
for (FieldError fieldError : bindingResult.getFieldErrors()) {
String msgUser = messageSource.getMessage(fieldError, LocaleContextHolder.getLocale());
String msgDev = fieldError.toString();
erros.add(new Error(msgUser, msgDev));
}
return erros;
}
use of org.springframework.validation.FieldError in project flytecnologia-api by jullierme.
the class FlyExceptionHandler method handleDataIntegrityViolationException.
@ExceptionHandler(DataIntegrityViolationException.class)
public ResponseEntity<Object> handleDataIntegrityViolationException(DataIntegrityViolationException ex, WebRequest request) {
String fieldError = "resource.operation-not-allowed";
if (ex.getCause() instanceof ConstraintViolationException) {
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 flytecnologia-api by jullierme.
the class FlyExceptionHandler method getListOfErros.
private List<Error> getListOfErros(String fieldError, Exception ex) {
String msgUser = getMessage(fieldError);
String msgDev = ex != null ? ExceptionUtils.getRootCauseMessage(ex) : "";
return Arrays.asList(new Error(msgUser, msgDev));
}
use of org.springframework.validation.FieldError in project spring-framework by spring-projects.
the class EscapedErrorsTests method testEscapedErrors.
@Test
public void testEscapedErrors() {
TestBean tb = new TestBean();
tb.setName("empty &");
Errors errors = new EscapedErrors(new BindException(tb, "tb"));
errors.rejectValue("name", "NAME_EMPTY &", null, "message: &");
errors.rejectValue("age", "AGE_NOT_SET <tag>", null, "message: <tag>");
errors.rejectValue("age", "AGE_NOT_32 <tag>", null, "message: <tag>");
errors.reject("GENERAL_ERROR \" '", null, "message: \" '");
assertThat(errors.hasErrors()).as("Correct errors flag").isTrue();
assertThat(errors.getErrorCount() == 4).as("Correct number of errors").isTrue();
assertThat("tb".equals(errors.getObjectName())).as("Correct object name").isTrue();
assertThat(errors.hasGlobalErrors()).as("Correct global errors flag").isTrue();
assertThat(errors.getGlobalErrorCount() == 1).as("Correct number of global errors").isTrue();
ObjectError globalError = errors.getGlobalError();
String defaultMessage = globalError.getDefaultMessage();
assertThat("message: " '".equals(defaultMessage)).as("Global error message escaped").isTrue();
assertThat("GENERAL_ERROR \" '".equals(globalError.getCode())).as("Global error code not escaped").isTrue();
ObjectError globalErrorInList = errors.getGlobalErrors().get(0);
assertThat(defaultMessage.equals(globalErrorInList.getDefaultMessage())).as("Same global error in list").isTrue();
ObjectError globalErrorInAllList = errors.getAllErrors().get(3);
assertThat(defaultMessage.equals(globalErrorInAllList.getDefaultMessage())).as("Same global error in list").isTrue();
assertThat(errors.hasFieldErrors()).as("Correct field errors flag").isTrue();
assertThat(errors.getFieldErrorCount() == 3).as("Correct number of field errors").isTrue();
assertThat(errors.getFieldErrors().size() == 3).as("Correct number of field errors in list").isTrue();
FieldError fieldError = errors.getFieldError();
assertThat("NAME_EMPTY &".equals(fieldError.getCode())).as("Field error code not escaped").isTrue();
assertThat("empty &".equals(errors.getFieldValue("name"))).as("Field value escaped").isTrue();
FieldError fieldErrorInList = errors.getFieldErrors().get(0);
assertThat(fieldError.getDefaultMessage().equals(fieldErrorInList.getDefaultMessage())).as("Same field error in list").isTrue();
assertThat(errors.hasFieldErrors("name")).as("Correct name errors flag").isTrue();
assertThat(errors.getFieldErrorCount("name") == 1).as("Correct number of name errors").isTrue();
assertThat(errors.getFieldErrors("name").size() == 1).as("Correct number of name errors in list").isTrue();
FieldError nameError = errors.getFieldError("name");
assertThat("message: &".equals(nameError.getDefaultMessage())).as("Name error message escaped").isTrue();
assertThat("NAME_EMPTY &".equals(nameError.getCode())).as("Name error code not escaped").isTrue();
assertThat("empty &".equals(errors.getFieldValue("name"))).as("Name value escaped").isTrue();
FieldError nameErrorInList = errors.getFieldErrors("name").get(0);
assertThat(nameError.getDefaultMessage().equals(nameErrorInList.getDefaultMessage())).as("Same name error in list").isTrue();
assertThat(errors.hasFieldErrors("age")).as("Correct age errors flag").isTrue();
assertThat(errors.getFieldErrorCount("age") == 2).as("Correct number of age errors").isTrue();
assertThat(errors.getFieldErrors("age").size() == 2).as("Correct number of age errors in list").isTrue();
FieldError ageError = errors.getFieldError("age");
assertThat("message: <tag>".equals(ageError.getDefaultMessage())).as("Age error message escaped").isTrue();
assertThat("AGE_NOT_SET <tag>".equals(ageError.getCode())).as("Age error code not escaped").isTrue();
assertThat((Integer.valueOf(0)).equals(errors.getFieldValue("age"))).as("Age value not escaped").isTrue();
FieldError ageErrorInList = errors.getFieldErrors("age").get(0);
assertThat(ageError.getDefaultMessage().equals(ageErrorInList.getDefaultMessage())).as("Same name error in list").isTrue();
FieldError ageError2 = errors.getFieldErrors("age").get(1);
assertThat("message: <tag>".equals(ageError2.getDefaultMessage())).as("Age error 2 message escaped").isTrue();
assertThat("AGE_NOT_32 <tag>".equals(ageError2.getCode())).as("Age error 2 code not escaped").isTrue();
}
use of org.springframework.validation.FieldError in project spring-boot by spring-projects.
the class ValidationBindHandlerTests method bindShouldFailIfExistingValueIsInvalid.
@Test
void bindShouldFailIfExistingValueIsInvalid() {
ExampleValidatedBean existingValue = new ExampleValidatedBean();
BindValidationException cause = bindAndExpectValidationError(() -> this.binder.bind(ConfigurationPropertyName.of("foo"), Bindable.of(ExampleValidatedBean.class).withExistingValue(existingValue), this.handler));
FieldError fieldError = (FieldError) cause.getValidationErrors().getAllErrors().get(0);
assertThat(fieldError.getField()).isEqualTo("age");
}
Aggregations