Search in sources :

Example 26 with FieldError

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;
}
Also used : ArrayList(java.util.ArrayList) FieldError(org.springframework.validation.FieldError) FieldError(org.springframework.validation.FieldError)

Example 27 with FieldError

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);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) FieldError(org.springframework.validation.FieldError) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseEntityExceptionHandler(org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler)

Example 28 with FieldError

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));
}
Also used : FieldError(org.springframework.validation.FieldError)

Example 29 with FieldError

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: &quot; &#39;".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 &amp;".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: &amp;".equals(nameError.getDefaultMessage())).as("Name error message escaped").isTrue();
    assertThat("NAME_EMPTY &".equals(nameError.getCode())).as("Name error code not escaped").isTrue();
    assertThat("empty &amp;".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: &lt;tag&gt;".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: &lt;tag&gt;".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();
}
Also used : Errors(org.springframework.validation.Errors) ObjectError(org.springframework.validation.ObjectError) TestBean(org.springframework.beans.testfixture.beans.TestBean) BindException(org.springframework.validation.BindException) FieldError(org.springframework.validation.FieldError) Test(org.junit.jupiter.api.Test)

Example 30 with FieldError

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");
}
Also used : FieldError(org.springframework.validation.FieldError) Test(org.junit.jupiter.api.Test)

Aggregations

FieldError (org.springframework.validation.FieldError)101 Test (org.junit.jupiter.api.Test)24 ObjectError (org.springframework.validation.ObjectError)20 BindingResult (org.springframework.validation.BindingResult)19 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)17 ApiOperation (io.swagger.annotations.ApiOperation)13 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)12 Errors (org.springframework.validation.Errors)11 ArrayList (java.util.ArrayList)10 BindException (org.springframework.validation.BindException)7 Locale (java.util.Locale)6 CustomResult (com.megagao.production.ssm.domain.customize.CustomResult)5 Test (org.junit.Test)5 MapBindingResult (org.springframework.validation.MapBindingResult)5 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)5 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)5 JsonResult (com.odysseusinc.arachne.commons.api.v1.dto.util.JsonResult)4 Method (java.lang.reflect.Method)4 HashMap (java.util.HashMap)4 ErrorMessage (org.springframework.cloud.servicebroker.model.error.ErrorMessage)4