Refine SyncInvocableHandlerMethod error handling

Ensure the error is wrapped as ServerErrorException
This commit is contained in:
Rossen Stoyanchev
2018-03-31 12:03:03 -04:00
parent 912c270f2b
commit d9e17a62ce
4 changed files with 60 additions and 16 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@ import org.springframework.ui.Model;
import org.springframework.validation.support.BindingAwareConcurrentModel;
import org.springframework.web.bind.support.WebBindingInitializer;
import org.springframework.web.bind.support.WebExchangeDataBinder;
import org.springframework.web.server.ServerErrorException;
import org.springframework.web.server.ServerWebExchange;
/**
@@ -75,6 +76,7 @@ public class BindingContext {
* @param target the object to create a data binder for
* @param name the name of the target object
* @return the created data binder
* @throws ServerErrorException if {@code @InitBinder} method invocation fails
*/
public WebExchangeDataBinder createDataBinder(ServerWebExchange exchange, @Nullable Object target, String name) {
WebExchangeDataBinder dataBinder = new WebExchangeDataBinder(target, name);
@@ -86,6 +88,7 @@ public class BindingContext {
/**
* Initialize the data binder instance for the given exchange.
* @throws ServerErrorException if {@code @InitBinder} method invocation fails
*/
protected WebExchangeDataBinder initDataBinder(WebExchangeDataBinder binder, ServerWebExchange exchange) {
return binder;
@@ -97,6 +100,7 @@ public class BindingContext {
* @param exchange the current exchange
* @param name the name of the target object
* @return the created data binder
* @throws ServerErrorException if {@code @InitBinder} method invocation fails
*/
public WebExchangeDataBinder createDataBinder(ServerWebExchange exchange, String name) {
return createDataBinder(exchange, null, name);

View File

@@ -29,6 +29,7 @@ import org.springframework.lang.Nullable;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.reactive.BindingContext;
import org.springframework.web.reactive.HandlerResult;
import org.springframework.web.server.ServerErrorException;
import org.springframework.web.server.ServerWebExchange;
/**
@@ -95,6 +96,7 @@ public class SyncInvocableHandlerMethod extends HandlerMethod {
* @param bindingContext the binding context to use
* @param providedArgs optional list of argument values to match by type
* @return Mono with a {@link HandlerResult}.
* @throws ServerErrorException if method argument resolution or method invocation fails
*/
@Nullable
public HandlerResult invokeForHandlerResult(ServerWebExchange exchange,
@@ -104,9 +106,10 @@ public class SyncInvocableHandlerMethod extends HandlerMethod {
this.delegate.invoke(exchange, bindingContext, providedArgs).subscribeWith(processor);
if (processor.isTerminated()) {
Throwable error = processor.getError();
if (error != null) {
throw (RuntimeException) error;
Throwable ex = processor.getError();
if (ex != null) {
throw (ex instanceof ServerErrorException ? (ServerErrorException) ex :
new ServerErrorException("Failed to invoke: " + getShortLogMessage(), this, ex));
}
return processor.peek();
}

View File

@@ -92,7 +92,7 @@ public class PathVariableMethodArgumentResolver extends AbstractNamedValueSyncAr
@Override
protected void handleMissingValue(String name, MethodParameter parameter) {
throw new ServerErrorException(name, parameter);
throw new ServerErrorException(name, parameter, null);
}
@Override