diff --git a/http-server-jetty/src/test/groovy/io/micronaut/servlet/jetty/JettyExceptionHandlerSpec.groovy b/http-server-jetty/src/test/groovy/io/micronaut/servlet/jetty/JettyExceptionHandlerSpec.groovy index c2393e005..d900d5747 100644 --- a/http-server-jetty/src/test/groovy/io/micronaut/servlet/jetty/JettyExceptionHandlerSpec.groovy +++ b/http-server-jetty/src/test/groovy/io/micronaut/servlet/jetty/JettyExceptionHandlerSpec.groovy @@ -3,17 +3,23 @@ package io.micronaut.servlet.jetty import groovy.transform.InheritConstructors import io.micronaut.context.annotation.Property import io.micronaut.context.annotation.Requires +import io.micronaut.core.async.publisher.Publishers +import io.micronaut.http.HttpAttributes import io.micronaut.http.HttpRequest import io.micronaut.http.HttpResponse import io.micronaut.http.HttpStatus import io.micronaut.http.MutableHttpResponse import io.micronaut.http.annotation.Controller +import io.micronaut.http.annotation.Filter import io.micronaut.http.annotation.Get import io.micronaut.http.client.HttpClient import io.micronaut.http.client.annotation.Client import io.micronaut.http.client.exceptions.HttpClientResponseException +import io.micronaut.http.filter.OncePerRequestHttpServerFilter +import io.micronaut.http.filter.ServerFilterChain import io.micronaut.http.server.exceptions.ExceptionHandler import io.micronaut.test.extensions.spock.annotation.MicronautTest +import org.reactivestreams.Publisher import spock.lang.Specification import javax.inject.Inject @@ -45,6 +51,15 @@ class JettyExceptionHandlerSpec extends Specification { ex.response.getBody().get() == "hello" } + void "test a filter throwing an exception on a 404"() { + when: + client.toBlocking().retrieve("/exception/doesnt-exist") + + then: + def ex = thrown(HttpClientResponseException) + ex.response.status() == HttpStatus.UNAUTHORIZED + } + @Controller("/exception") static class ExceptionController { @@ -78,4 +93,30 @@ class JettyExceptionHandlerSpec extends Specification { @InheritConstructors static class MyException extends Exception {} + + @Singleton + @Filter("/**") + @Requires(property = "spec.name", value = "JettyExceptionHandlerSpec") + static class MyFilter extends OncePerRequestHttpServerFilter { + + @Override + protected Publisher> doFilterOnce(HttpRequest request, ServerFilterChain chain) { + if (!request.getAttribute(HttpAttributes.ROUTE_MATCH).isPresent()) { + return Publishers.just(new Unauthorized()) + } + chain.proceed(request) + } + } + + @InheritConstructors + static class Unauthorized extends Exception {} + + @Singleton + @Requires(property = "spec.name", value = "JettyExceptionHandlerSpec") + static class UnauthorizedExceptionHandler implements ExceptionHandler> { + @Override + MutableHttpResponse handle(HttpRequest request, Unauthorized exception) { + return HttpResponse.unauthorized() + } + } } diff --git a/servlet-core/src/main/java/io/micronaut/servlet/http/ServletHttpHandler.java b/servlet-core/src/main/java/io/micronaut/servlet/http/ServletHttpHandler.java index 3b7962783..4b043cdc4 100644 --- a/servlet-core/src/main/java/io/micronaut/servlet/http/ServletHttpHandler.java +++ b/servlet-core/src/main/java/io/micronaut/servlet/http/ServletHttpHandler.java @@ -276,7 +276,7 @@ private void emitError(ServletExchange exchange, req, res, null, - true, + false, exchange, responsePublisher, AnnotationMetadata.EMPTY_METADATA @@ -743,9 +743,7 @@ private void handleException( emitter.onComplete(); })); } - } else { - RouteMatch errorRoute = lookupErrorRoute(route, e); if (errorRoute == null) { if (e instanceof CodecException) {