Remove DispatcherHandlerExceptionMapper

No need to translate to response status codes any more and no need for
an exception handling mechanism at this level.
This commit is contained in:
Rossen Stoyanchev 2016-04-19 15:55:34 -04:00
parent 8f0fe713f5
commit 3849fd6b5b
3 changed files with 6 additions and 173 deletions

View File

@ -19,7 +19,6 @@ package org.springframework.web.reactive;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
@ -31,9 +30,11 @@ import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.server.WebHandler;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebHandler;
/**
* Central dispatcher for HTTP request handlers/controllers. Dispatches to registered
@ -64,29 +65,12 @@ public class DispatcherHandler implements WebHandler, ApplicationContextAware {
private List<HandlerResultHandler> resultHandlers;
private Function<Throwable, Throwable> errorMapper = new DispatcherHandlerExceptionMapper();
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
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) {
@ -121,8 +105,7 @@ public class DispatcherHandler implements WebHandler, ApplicationContextAware {
.concatMap(mapping -> mapping.getHandler(exchange))
.next()
.then(handler -> invokeHandler(exchange, handler))
.then(result -> handleResult(exchange, result))
.otherwise(ex -> Mono.error(this.errorMapper.apply(ex)));
.then(result -> handleResult(exchange, result));
}
private Mono<HandlerResult> invokeHandler(ServerWebExchange exchange, Object handler) {
@ -153,7 +136,8 @@ public class DispatcherHandler implements WebHandler, ApplicationContextAware {
private static class NotFoundHandlerMapping implements HandlerMapping {
@SuppressWarnings("ThrowableInstanceNeverThrown")
private static final Exception HANDLER_NOT_FOUND_EXCEPTION = new HandlerNotFoundException();
private static final Exception HANDLER_NOT_FOUND_EXCEPTION =
new ResponseStatusException(HttpStatus.NOT_FOUND, "No matching handler");
@Override

View File

@ -1,65 +0,0 @@
/*
* 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.HttpMediaTypeNotAcceptableException;
import org.springframework.web.server.ResponseStatusException;
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;
}
}

View File

@ -1,86 +0,0 @@
/*
* 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.HttpStatus;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.server.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();
ex = this.mapper.apply(ex);
assertEquals(ResponseStatusException.class, ex.getClass());
assertEquals(HttpStatus.NOT_FOUND, ((ResponseStatusException) ex).getStatus());
}
@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).getStatus());
}
@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).getStatus());
}
@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).getStatus());
}
@ResponseStatus(code = HttpStatus.BAD_REQUEST)
private static class ResponseStatusAnnotatedException extends Exception {
}
}