Skip to content

Commit

Permalink
Fix exception handler lookup logic (#90)
Browse files Browse the repository at this point in the history
* Fix exception handler lookup logic. Fixes #88

* Fix tests
  • Loading branch information
jameskleeh authored Oct 26, 2020
1 parent f357f14 commit 54b2159
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package io.micronaut.servlet.jetty

import groovy.transform.InheritConstructors
import io.micronaut.context.annotation.Property
import io.micronaut.context.annotation.Requires
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.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.server.exceptions.ExceptionHandler
import io.micronaut.test.extensions.spock.annotation.MicronautTest
import spock.lang.Specification

import javax.inject.Inject
import javax.inject.Singleton

@MicronautTest
@Property(name = "spec.name", value = "JettyExceptionHandlerSpec")
class JettyExceptionHandlerSpec extends Specification {

@Inject
@Client("/")
HttpClient client

void "test an exception handler for mutable request works"() {
when:
def resp = client.toBlocking().exchange("/exception")

then:
resp.status() == HttpStatus.OK
}

void "test an exception handler returning the body"() {
when:
client.toBlocking().retrieve("/exception/my")

then:
def ex = thrown(HttpClientResponseException)
ex.response.status() == HttpStatus.INTERNAL_SERVER_ERROR
ex.response.getBody().get() == "hello"
}

@Controller("/exception")
static class ExceptionController {

@Get
void throwsEx() {
throw new RuntimeException("bad")
}

@Get("/my")
void throwsMy() {
throw new MyException("bad")
}
}

@Singleton
static class MyExceptionHandler implements ExceptionHandler<MyException, String> {
@Override
String handle(HttpRequest request, MyException exception) {
"hello"
}
}

@Singleton
@Requires(property = "spec.name", value = "JettyExceptionHandlerSpec")
static class RuntimeExceptionHandler implements ExceptionHandler<RuntimeException, MutableHttpResponse<?>> {
@Override
MutableHttpResponse<?> handle(HttpRequest request, RuntimeException exception) {
return HttpResponse.ok()
}
}

@InheritConstructors
static class MyException extends Exception {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -825,7 +825,7 @@ private JsonError newJsonError(HttpRequest<Object> req, String message) {
@SuppressWarnings("unchecked")
private ExceptionHandler<Throwable, ?> lookupExceptionHandler(Throwable e) {
final Class<? extends Throwable> type = e.getClass();
return applicationContext.findBean(ExceptionHandler.class, Qualifiers.byTypeArgumentsClosest(type, HttpResponse.class))
return applicationContext.findBean(ExceptionHandler.class, Qualifiers.byTypeArgumentsClosest(type, Object.class))
.orElse(null);
}

Expand Down

0 comments on commit 54b2159

Please sign in to comment.