org.springframework.web.bind.support.WebExchangeBindException Java Examples

The following examples show how to use org.springframework.web.bind.support.WebExchangeBindException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ModelAttributeMethodArgumentResolverTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private void testValidationError(MethodParameter param, Function<Mono<?>, Mono<?>> valueMonoExtractor)
		throws URISyntaxException {

	ServerWebExchange exchange = postForm("age=invalid");
	Mono<?> mono = createResolver().resolveArgument(param, this.bindContext, exchange);
	mono = valueMonoExtractor.apply(mono);

	StepVerifier.create(mono)
			.consumeErrorWith(ex -> {
				assertTrue(ex instanceof WebExchangeBindException);
				WebExchangeBindException bindException = (WebExchangeBindException) ex;
				assertEquals(1, bindException.getErrorCount());
				assertTrue(bindException.hasFieldErrors("age"));
			})
			.verify();
}
 
Example #2
Source File: WebExchangeBindExceptionHandler.java    From staccato with Apache License 2.0 6 votes vote down vote up
@Override
protected String getMessage(WebExchangeBindException ex) {
    List<ObjectError> errors = ex.getAllErrors();
    if (!errors.isEmpty()) {
        StringBuilder sb = new StringBuilder();
        Iterator<ObjectError> it = errors.iterator();
        while (it.hasNext()) {
            sb.append(it.next().getDefaultMessage());
            if (it.hasNext()) {
                sb.append(" ");
            }
        }
        return sb.toString();
    }
    return ex.getMessage();
}
 
Example #3
Source File: ServiceBrokerWebFluxExceptionHandlerTest.java    From spring-cloud-open-service-broker with Apache License 2.0 6 votes vote down vote up
@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");
}
 
Example #4
Source File: ModelAttributeMethodArgumentResolverTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
private void testValidationError(MethodParameter param, Function<Mono<?>, Mono<?>> valueMonoExtractor)
		throws URISyntaxException {

	ServerWebExchange exchange = postForm("age=invalid");
	Mono<?> mono = createResolver().resolveArgument(param, this.bindContext, exchange);
	mono = valueMonoExtractor.apply(mono);

	StepVerifier.create(mono)
			.consumeErrorWith(ex -> {
				assertTrue(ex instanceof WebExchangeBindException);
				WebExchangeBindException bindException = (WebExchangeBindException) ex;
				assertEquals(1, bindException.getErrorCount());
				assertTrue(bindException.hasFieldErrors("age"));
			})
			.verify();
}
 
Example #5
Source File: ConventionBasedSpringValidationErrorToApiErrorHandlerListenerTest.java    From backstopper with Apache License 2.0 6 votes vote down vote up
@DataProvider(value = {
    "true",
    "false"
})
@Test
public void shouldHandleException_ignores_WebExchangeBindException_that_has_null_or_empty_ObjectError_list(
    boolean objectErrorsListIsNull
) {
    // given
    BindingResult bindingResult = mock(BindingResult.class);

    List<ObjectError> errorsList = (objectErrorsListIsNull) ? null : Collections.emptyList();
    when(bindingResult.getAllErrors()).thenReturn(errorsList);

    WebExchangeBindException ex = new WebExchangeBindException(null, bindingResult);

    // when
    ApiExceptionHandlerListenerResult result = listener.shouldHandleException(ex);

    // then
    validateResponse(result, false, null);
    verify(bindingResult).getAllErrors();
}
 
Example #6
Source File: BindAdviceTrait.java    From problem-spring-web with MIT License 5 votes vote down vote up
@API(status = INTERNAL)
@ExceptionHandler
default Mono<ResponseEntity<Problem>> handleBindingResult(
        final WebExchangeBindException exception,
        final ServerWebExchange request) {
    return newConstraintViolationProblem(exception, createViolations(exception), request);
}
 
Example #7
Source File: AbstractMessageReaderArgumentResolver.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void validate(Object target, Object[] validationHints, MethodParameter param,
		BindingContext binding, ServerWebExchange exchange) {

	String name = Conventions.getVariableNameForParameter(param);
	WebExchangeDataBinder binder = binding.createDataBinder(exchange, target, name);
	binder.validate(validationHints);
	if (binder.getBindingResult().hasErrors()) {
		throw new WebExchangeBindException(param, binder.getBindingResult());
	}
}
 
Example #8
Source File: ConventionBasedSpringValidationErrorToApiErrorHandlerListenerTest.java    From backstopper with Apache License 2.0 5 votes vote down vote up
@Test
public void extractAllErrorsFromWebExchangeBindException_returns_null_if_extraction_method_is_null() {
    // given
    ConventionBasedSpringValidationErrorToApiErrorHandlerListener listenerSpy = spy(listener);
    doReturn(null).when(listenerSpy).getWebExchangeBindExGetAllErrorsMethod();

    // when
    List<ObjectError> result = listenerSpy.extractAllErrorsFromWebExchangeBindException(
        mock(WebExchangeBindException.class)
    );

    // then
    assertThat(result).isNull();
    verify(listenerSpy).getWebExchangeBindExGetAllErrorsMethod();
}
 
Example #9
Source File: ConventionBasedSpringValidationErrorToApiErrorHandlerListenerTest.java    From backstopper with Apache License 2.0 5 votes vote down vote up
@Test
public void extractAllErrorsFromWebExchangeBindException_works_as_expected_for_WebExchangeBindException() {
    // given
    WebExchangeBindException exMock = mock(WebExchangeBindException.class);
    List<ObjectError> expectedErrorsList = mock(List.class);

    doReturn(expectedErrorsList).when(exMock).getAllErrors();

    // when
    List<ObjectError> result = listener.extractAllErrorsFromWebExchangeBindException(exMock);

    // then
    assertThat(result).isSameAs(expectedErrorsList);
}
 
Example #10
Source File: ConventionBasedSpringValidationErrorToApiErrorHandlerListenerTest.java    From backstopper with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldHandleException_delegates_to_extractAllErrorsFromWebExchangeBindException_for_WebExchangeBindException_error_retrieval() {
    // given
    ConventionBasedSpringValidationErrorToApiErrorHandlerListener listenerSpy = spy(listener);

    WebExchangeBindException ex = new WebExchangeBindException(null, mock(BindingResult.class));

    ApiError someFieldError = testProjectApiErrors.getMissingExpectedContentApiError();
    ApiError otherFieldError = testProjectApiErrors.getTypeConversionApiError();
    ApiError notAFieldError = testProjectApiErrors.getGenericBadRequestApiError();
    List<ObjectError> errorsList = Arrays.asList(
        new FieldError("someObj", "someField", someFieldError.getName()),
        new FieldError("otherObj", "otherField", otherFieldError.getName()),
        new ObjectError("notAFieldObject", notAFieldError.getName())
    );

    doReturn(errorsList).when(listenerSpy).extractAllErrorsFromWebExchangeBindException(ex);

    // when
    ApiExceptionHandlerListenerResult result = listenerSpy.shouldHandleException(ex);

    // then
    validateResponse(result, true, Arrays.asList(
        new ApiErrorWithMetadata(someFieldError, Pair.of("field", "someField")),
        new ApiErrorWithMetadata(otherFieldError, Pair.of("field", "otherField")),
        notAFieldError
    ));
    verify(listenerSpy).extractAllErrorsFromWebExchangeBindException(ex);
}
 
Example #11
Source File: ConventionBasedSpringValidationErrorToApiErrorHandlerListenerTest.java    From backstopper with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldHandleException_handles_WebExchangeBindException_as_expected() {
    // given
    BindingResult bindingResult = mock(BindingResult.class);

    ApiError someFieldError = testProjectApiErrors.getMissingExpectedContentApiError();
    ApiError otherFieldError = testProjectApiErrors.getTypeConversionApiError();
    ApiError notAFieldError = testProjectApiErrors.getGenericBadRequestApiError();
    List<ObjectError> errorsList = Arrays.asList(
        new FieldError("someObj", "someField", someFieldError.getName()),
        new FieldError("otherObj", "otherField", otherFieldError.getName()),
        new ObjectError("notAFieldObject", notAFieldError.getName())
    );
    when(bindingResult.getAllErrors()).thenReturn(errorsList);

    WebExchangeBindException ex = new WebExchangeBindException(null, bindingResult);

    // when
    ApiExceptionHandlerListenerResult result = listener.shouldHandleException(ex);

    // then
    validateResponse(result, true, Arrays.asList(
        new ApiErrorWithMetadata(someFieldError, Pair.of("field", "someField")),
        new ApiErrorWithMetadata(otherFieldError, Pair.of("field", "otherField")),
        notAFieldError
    ));
    verify(bindingResult).getAllErrors();
}
 
Example #12
Source File: ConventionBasedSpringValidationErrorToApiErrorHandlerListenerTest.java    From backstopper with Apache License 2.0 5 votes vote down vote up
@Test
public void extractGetAllErrorsMethod_works_as_expected_for_WebExchangeBindException()
    throws NoSuchMethodException {
    
    // when
    Method result = extractGetAllErrorsMethod(WEB_EXCHANGE_BIND_EXCEPTION_CLASSNAME);

    // then
    assertThat(result).isEqualTo(WebExchangeBindException.class.getDeclaredMethod("getAllErrors"));
}
 
Example #13
Source File: ResponseStatusWebErrorHandlerTest.java    From errors-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
private Object[] paramsForCanHandle() {
    return p(
        p(mock(UnsupportedMediaTypeStatusException.class), true),
        p(mock(WebExchangeBindException.class), true),
        p(mock(ServerWebInputException.class), true),
        p(new RuntimeException(), false),
        p(null, false)
    );
}
 
Example #14
Source File: AbstractMessageReaderArgumentResolver.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void validate(Object target, Object[] validationHints, MethodParameter param,
		BindingContext binding, ServerWebExchange exchange) {

	String name = Conventions.getVariableNameForParameter(param);
	WebExchangeDataBinder binder = binding.createDataBinder(exchange, target, name);
	binder.validate(validationHints);
	if (binder.getBindingResult().hasErrors()) {
		throw new WebExchangeBindException(param, binder.getBindingResult());
	}
}
 
Example #15
Source File: ConventionBasedSpringValidationErrorToApiErrorHandlerListenerTest.java    From backstopper with Apache License 2.0 4 votes vote down vote up
@Test
public void const_WEB_EXCHANGE_BIND_EXCEPTION_CLASSNAME_equals_WebExchangeBindException() {
    // expect
    assertThat(WEB_EXCHANGE_BIND_EXCEPTION_CLASSNAME).isEqualTo(WebExchangeBindException.class.getName());
}
 
Example #16
Source File: WebExchangeBindExceptionHandler.java    From staccato with Apache License 2.0 4 votes vote down vote up
public WebExchangeBindExceptionHandler() {
    super(WebExchangeBindException.class.getSimpleName());
}
 
Example #17
Source File: ServiceBrokerWebFluxExceptionHandler.java    From spring-cloud-open-service-broker with Apache License 2.0 2 votes vote down vote up
/**
 * Handle a {@link WebExchangeBindException}
 *
 * @param ex the exception
 * @return an error message
 */
@ExceptionHandler(WebExchangeBindException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ErrorMessage handleException(WebExchangeBindException ex) {
	return handleBindingException(ex, ex.getBindingResult());
}