提交 c13a8c9b 编写于 作者: R Rossen Stoyanchev

DispatcherHandler maps errors to ResponseStatusException

The DispatcherHandler now has an errorMapper property that is a
function for transforming errors. By default this property is set to an
instance of DispatcherHandlerExceptionMapper which wraps "standard"
framework exceptions and @ResponseStatus-annotated exceptions as
ResponseStatusException.

This makes it easy to handle the exceptions downstream uniformly.
上级 1f15b7e0
...@@ -19,11 +19,14 @@ package org.springframework.web.reactive; ...@@ -19,11 +19,14 @@ package org.springframework.web.reactive;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.function.Function;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.reactivestreams.Publisher; import org.reactivestreams.Publisher;
import org.reactivestreams.Subscriber;
import reactor.Publishers; import reactor.Publishers;
import reactor.fn.BiConsumer;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.beans.factory.BeanFactoryUtils;
...@@ -63,12 +66,30 @@ public class DispatcherHandler implements HttpHandler, ApplicationContextAware { ...@@ -63,12 +66,30 @@ public class DispatcherHandler implements HttpHandler, ApplicationContextAware {
private List<HandlerResultHandler> resultHandlers; private List<HandlerResultHandler> resultHandlers;
private Function<Throwable, Throwable> errorMapper = new DispatcherHandlerExceptionMapper();
@Override @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
initStrategies(applicationContext); initStrategies(applicationContext);
} }
/**
* Configure a function to map error signals from the {@code DispatcherHandler}.
* <p>By default this is set to {@link DispatcherHandlerExceptionMapper}.
* @param errorMapper the function
*/
public void setErrorMapper(Function<Throwable, Throwable> errorMapper) {
this.errorMapper = errorMapper;
}
/**
* Return the configured function for mapping exceptions.
*/
public Function<Throwable, Throwable> getErrorMapper() {
return this.errorMapper;
}
protected void initStrategies(ApplicationContext context) { protected void initStrategies(ApplicationContext context) {
Map<String, HandlerMapping> mappingBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors( Map<String, HandlerMapping> mappingBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(
...@@ -107,10 +128,12 @@ public class DispatcherHandler implements HttpHandler, ApplicationContextAware { ...@@ -107,10 +128,12 @@ public class DispatcherHandler implements HttpHandler, ApplicationContextAware {
return handlerAdapter.handle(request, response, handler); return handlerAdapter.handle(request, response, handler);
}); });
return Publishers.concatMap(resultPublisher, result -> { Publisher<Void> completionPublisher = Publishers.concatMap(resultPublisher, result -> {
HandlerResultHandler handler = getResultHandler(result); HandlerResultHandler handler = getResultHandler(result);
return handler.handleResult(request, response, result); return handler.handleResult(request, response, result);
}); });
return mapError(completionPublisher, this.errorMapper);
} }
protected HandlerAdapter getHandlerAdapter(Object handler) { protected HandlerAdapter getHandlerAdapter(Object handler) {
...@@ -139,6 +162,15 @@ public class DispatcherHandler implements HttpHandler, ApplicationContextAware { ...@@ -139,6 +162,15 @@ public class DispatcherHandler implements HttpHandler, ApplicationContextAware {
}); });
} }
private static <E> Publisher<E> mapError(Publisher<E> source, Function<Throwable, Throwable> function) {
return Publishers.lift(source, null, new BiConsumer<Throwable, Subscriber<? super E>>() {
@Override
public void accept(Throwable throwable, Subscriber<? super E> subscriber) {
subscriber.onError(function.apply(throwable));
}
}, null);
}
private static class NotFoundHandlerMapping implements HandlerMapping { private static class NotFoundHandlerMapping implements HandlerMapping {
@Override @Override
......
/*
* Copyright 2002-2015 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.reactive;
import java.util.function.Function;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.ResponseStatusException;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.bind.annotation.ResponseStatus;
/**
* Map "standard" framework exceptions and
* {@link ResponseStatus @ResponseStatus}-annotated exceptions to a
* {@link ResponseStatusException}.
*
* @author Rossen Stoyanchev
*/
public class DispatcherHandlerExceptionMapper implements Function<Throwable, Throwable> {
@Override
public Throwable apply(Throwable ex) {
if (ex instanceof HandlerNotFoundException) {
ex = new ResponseStatusException(HttpStatus.NOT_FOUND, ex);
}
else if (ex instanceof HttpMediaTypeNotAcceptableException) {
ex = new ResponseStatusException(HttpStatus.NOT_ACCEPTABLE, ex);
}
else {
ResponseStatus status = findStatus(ex);
if (status != null) {
ex = new ResponseStatusException(status.code(), ex);
}
}
return ex;
}
private ResponseStatus findStatus(Throwable ex) {
Class<? extends Throwable> type = ex.getClass();
ResponseStatus status = AnnotatedElementUtils.findMergedAnnotation(type, ResponseStatus.class);
if (status != null) {
return status;
}
else if (ex.getCause() != null) {
return findStatus(ex.getCause());
}
return null;
}
}
...@@ -37,6 +37,7 @@ import org.springframework.core.convert.support.DefaultConversionService; ...@@ -37,6 +37,7 @@ import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.web.ResponseStatusException;
import org.springframework.http.server.reactive.ErrorHandlingHttpHandler; import org.springframework.http.server.reactive.ErrorHandlingHttpHandler;
import org.springframework.http.server.reactive.FilterChainHttpHandler; import org.springframework.http.server.reactive.FilterChainHttpHandler;
import org.springframework.http.server.reactive.HttpExceptionHandler; import org.springframework.http.server.reactive.HttpExceptionHandler;
...@@ -58,6 +59,7 @@ import org.springframework.web.reactive.method.annotation.ResponseBodyResultHand ...@@ -58,6 +59,7 @@ import org.springframework.web.reactive.method.annotation.ResponseBodyResultHand
import static org.hamcrest.CoreMatchers.startsWith; import static org.hamcrest.CoreMatchers.startsWith;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame; import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat; import static org.junit.Assert.assertThat;
...@@ -101,7 +103,9 @@ public class DispatcherHandlerErrorTests { ...@@ -101,7 +103,9 @@ public class DispatcherHandlerErrorTests {
Publisher<Void> publisher = this.dispatcherHandler.handle(this.request, this.response); Publisher<Void> publisher = this.dispatcherHandler.handle(this.request, this.response);
Throwable ex = awaitErrorSignal(publisher); Throwable ex = awaitErrorSignal(publisher);
assertEquals(HandlerNotFoundException.class, ex.getClass()); assertEquals(ResponseStatusException.class, ex.getClass());
assertNotNull(ex.getCause());
assertEquals(HandlerNotFoundException.class, ex.getCause().getClass());
} }
@Test @Test
...@@ -155,7 +159,9 @@ public class DispatcherHandlerErrorTests { ...@@ -155,7 +159,9 @@ public class DispatcherHandlerErrorTests {
Publisher<Void> publisher = this.dispatcherHandler.handle(this.request, this.response); Publisher<Void> publisher = this.dispatcherHandler.handle(this.request, this.response);
Throwable ex = awaitErrorSignal(publisher); Throwable ex = awaitErrorSignal(publisher);
assertEquals(HttpMediaTypeNotAcceptableException.class, ex.getClass()); assertEquals(ResponseStatusException.class, ex.getClass());
assertNotNull(ex.getCause());
assertEquals(HttpMediaTypeNotAcceptableException.class, ex.getCause().getClass());
} }
@Test @Test
......
/*
* Copyright 2002-2015 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.reactive;
import java.util.Collections;
import org.junit.Before;
import org.junit.Test;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.ResponseStatusException;
import org.springframework.web.bind.annotation.ResponseStatus;
import static org.junit.Assert.assertEquals;
/**
* @author Rossen Stoyanchev
*/
public class DispatcherHandlerExceptionMapperTests {
private DispatcherHandlerExceptionMapper mapper;
@Before
public void setUp() throws Exception {
this.mapper = new DispatcherHandlerExceptionMapper();
}
@Test
public void handlerNotFound() throws Exception {
Throwable ex = new HandlerNotFoundException(HttpMethod.GET, "/path", new HttpHeaders());
ex = this.mapper.apply(ex);
assertEquals(ResponseStatusException.class, ex.getClass());
assertEquals(HttpStatus.NOT_FOUND, ((ResponseStatusException) ex).getHttpStatus());
}
@Test
public void httpMediaTypeNotAcceptable() throws Exception {
Throwable ex = new HttpMediaTypeNotAcceptableException(Collections.emptyList());
ex = this.mapper.apply(ex);
assertEquals(ResponseStatusException.class, ex.getClass());
assertEquals(HttpStatus.NOT_ACCEPTABLE, ((ResponseStatusException) ex).getHttpStatus());
}
@Test
public void responseStatusAnnotation() throws Exception {
Throwable ex = new ResponseStatusAnnotatedException();
ex = this.mapper.apply(ex);
assertEquals(ResponseStatusException.class, ex.getClass());
assertEquals(HttpStatus.BAD_REQUEST, ((ResponseStatusException) ex).getHttpStatus());
}
@Test
public void responseStatusAnnotationOnRootCause() throws Exception {
Throwable ex = new Exception(new ResponseStatusAnnotatedException());
ex = this.mapper.apply(ex);
assertEquals(ResponseStatusException.class, ex.getClass());
assertEquals(HttpStatus.BAD_REQUEST, ((ResponseStatusException) ex).getHttpStatus());
}
@ResponseStatus(code = HttpStatus.BAD_REQUEST)
private static class ResponseStatusAnnotatedException extends Exception {
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册