Search in sources :

Example 71 with FieldError

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");
}
Also used : BindingResult(org.springframework.validation.BindingResult) MapBindingResult(org.springframework.validation.MapBindingResult) MapBindingResult(org.springframework.validation.MapBindingResult) FieldError(org.springframework.validation.FieldError) Method(java.lang.reflect.Method) MethodParameter(org.springframework.core.MethodParameter) ErrorMessage(org.springframework.cloud.servicebroker.model.error.ErrorMessage) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) Test(org.junit.Test)

Example 72 with FieldError

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");
}
Also used : BindingResult(org.springframework.validation.BindingResult) MapBindingResult(org.springframework.validation.MapBindingResult) MapBindingResult(org.springframework.validation.MapBindingResult) FieldError(org.springframework.validation.FieldError) Method(java.lang.reflect.Method) MethodParameter(org.springframework.core.MethodParameter) ErrorMessage(org.springframework.cloud.servicebroker.model.error.ErrorMessage) WebExchangeBindException(org.springframework.web.bind.support.WebExchangeBindException) Test(org.junit.Test)

Example 73 with FieldError

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

Example 74 with FieldError

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);
}
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 75 with FieldError

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

Aggregations

FieldError (org.springframework.validation.FieldError)92 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)20 ObjectError (org.springframework.validation.ObjectError)19 Test (org.junit.jupiter.api.Test)17 BindingResult (org.springframework.validation.BindingResult)16 ApiOperation (io.swagger.annotations.ApiOperation)14 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)12 Errors (org.springframework.validation.Errors)11 ArrayList (java.util.ArrayList)7 BindException (org.springframework.validation.BindException)6 CustomResult (com.megagao.production.ssm.domain.customize.CustomResult)5 Locale (java.util.Locale)5 Test (org.junit.Test)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 HashMap (java.util.HashMap)4 UserProfileDTO (com.odysseusinc.arachne.portal.api.v1.dto.UserProfileDTO)3 LinkedHashSet (java.util.LinkedHashSet)3 MapBindingResult (org.springframework.validation.MapBindingResult)3