Search in sources :

Example 96 with FieldError

use of org.springframework.validation.FieldError in project spring-cloud-open-service-broker by spring-cloud.

the class ServiceBrokerWebFluxExceptionHandlerTest method webExchangeBindException.

@Test
void webExchangeBindException() 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);
    ErrorMessage errorMessage = exceptionHandler.handleException(exception);
    assertThat(errorMessage.getError()).isNull();
    assertThat(errorMessage.getMessage()).contains("field1");
    assertThat(errorMessage.getMessage()).contains("field2");
}
Also used : MapBindingResult(org.springframework.validation.MapBindingResult) BindingResult(org.springframework.validation.BindingResult) 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.jupiter.api.Test)

Example 97 with FieldError

use of org.springframework.validation.FieldError in project arch-playground by BeneStem.

the class BadRequestException method badRequest.

public static BadRequestException badRequest(final Object target, final String targetName, final String fieldName, final String message) {
    final var error = new BeanPropertyBindingResult(target, targetName);
    error.addError(new FieldError(targetName, fieldName, message));
    return badRequest(error);
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) FieldError(org.springframework.validation.FieldError)

Example 98 with FieldError

use of org.springframework.validation.FieldError in project alien4cloud by alien4cloud.

the class RestTechnicalExceptionHandler method processValidationError.

@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public RestResponse<FieldErrorDTO[]> processValidationError(MethodArgumentNotValidException e) {
    BindingResult result = e.getBindingResult();
    List<FieldErrorDTO> errors = Lists.newArrayList();
    for (FieldError fieldError : result.getFieldErrors()) {
        errors.add(new FieldErrorDTO(fieldError.getField(), fieldError.getCode()));
    }
    return RestResponseBuilder.<FieldErrorDTO[]>builder().data(errors.toArray(new FieldErrorDTO[errors.size()])).error(RestErrorBuilder.builder(RestErrorCode.ILLEGAL_PARAMETER).message("Method argument is invalid " + e.getMessage()).build()).build();
}
Also used : BindingResult(org.springframework.validation.BindingResult) FieldError(org.springframework.validation.FieldError) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 99 with FieldError

use of org.springframework.validation.FieldError in project molgenis by molgenis.

the class RepositoryConstraintViolationExceptionTest method testGetFieldErrors.

@Test
void testGetFieldErrors() {
    EntityErrors entityErrors = mock(EntityErrors.class);
    List<FieldError> list = new ArrayList<>();
    list.add(new FieldError("name1", "field1", "message1"));
    list.add(new FieldError("name2", "field2", "message2"));
    when(entityErrors.getFieldErrors("field")).thenReturn(list);
    RepositoryConstraintViolationException exception = new RepositoryConstraintViolationException(entityErrors);
    assertEquals(exception.getFieldErrors("field"), list);
}
Also used : ArrayList(java.util.ArrayList) FieldError(org.springframework.validation.FieldError) Test(org.junit.jupiter.api.Test)

Example 100 with FieldError

use of org.springframework.validation.FieldError in project molgenis by molgenis.

the class RepositoryConstraintViolationExceptionTest method getFieldError.

@Test
void getFieldError() {
    EntityErrors entityErrors = mock(EntityErrors.class);
    FieldError fieldError = new FieldError("name1", "field", "message1");
    when(entityErrors.getFieldError()).thenReturn(fieldError);
    RepositoryConstraintViolationException exception = new RepositoryConstraintViolationException(entityErrors);
    assertEquals(exception.getFieldError(), fieldError);
}
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