Skip to content

Commit

Permalink
Fix exception handler not looked up for exception thrown from filter. F…
Browse files Browse the repository at this point in the history
…ixes #122
  • Loading branch information
jameskleeh committed Apr 5, 2021
1 parent a0e0bdd commit 96ba1ca
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {

Expand Down Expand Up @@ -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<MutableHttpResponse<?>> 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<Unauthorized, MutableHttpResponse<?>> {
@Override
MutableHttpResponse<?> handle(HttpRequest request, Unauthorized exception) {
return HttpResponse.unauthorized()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ private void emitError(ServletExchange<Req, Res> exchange,
req,
res,
null,
true,
false,
exchange,
responsePublisher,
AnnotationMetadata.EMPTY_METADATA
Expand Down Expand Up @@ -743,9 +743,7 @@ private void handleException(
emitter.onComplete();
}));
}

} else {

RouteMatch<Object> errorRoute = lookupErrorRoute(route, e);
if (errorRoute == null) {
if (e instanceof CodecException) {
Expand Down

0 comments on commit 96ba1ca

Please sign in to comment.