diff --git a/http-generator-jex/src/main/java/io/avaje/http/generator/jex/ControllerMethodWriter.java b/http-generator-jex/src/main/java/io/avaje/http/generator/jex/ControllerMethodWriter.java index 4d08fcb74..e043b299f 100644 --- a/http-generator-jex/src/main/java/io/avaje/http/generator/jex/ControllerMethodWriter.java +++ b/http-generator-jex/src/main/java/io/avaje/http/generator/jex/ControllerMethodWriter.java @@ -42,10 +42,14 @@ void writeRouting() { final PathSegments segments = method.pathSegments(); final String fullPath = segments.fullPath(); - if (isFilter) { + if (method.isErrorMethod()) { + writer.append(" routing.error(%s.class, this::_%s)", method.exceptionShortName(), method.simpleName()); + } else if (isFilter) { writer.append(" routing.filter(this::_%s)", method.simpleName()); } else { - writer.append(" routing.%s(\"%s\", this::_%s)", webMethod.name().toLowerCase(), fullPath, method.simpleName()); + writer.append( + " routing.%s(\"%s\", this::_%s)", + webMethod.name().toLowerCase(), fullPath, method.simpleName()); } List roles = method.roles(); @@ -65,14 +69,14 @@ void writeRouting() { void writeHandler(boolean requestScoped) { if (method.isErrorMethod()) { - writer.append(" private void _%s(Context ctx, %s ex)", method.simpleName(), method.exceptionShortName()); + writer.append(" private void _%s(Context ctx, %s ex) {", method.simpleName(), method.exceptionShortName()); } else if (isFilter) { - writer.append(" private void _%s(Context ctx, FilterChain chain)", method.simpleName()); + writer.append(" private void _%s(Context ctx, FilterChain chain) throws IOException {", method.simpleName()); } else { - writer.append(" private void _%s(Context ctx)", method.simpleName()); + writer.append(" private void _%s(Context ctx) throws IOException {", method.simpleName()); } - writer.append(" throws IOException {", method.simpleName()).eol(); + writer.eol(); write(requestScoped); writer.append(" }").eol().eol(); diff --git a/tests/pom.xml b/tests/pom.xml index 39c94d55e..8d146e655 100644 --- a/tests/pom.xml +++ b/tests/pom.xml @@ -15,7 +15,7 @@ 5.11.3 3.26.3 2.18.1 - 3.0-RC1 + 3.0-RC2 11.0 4.1.4 6.3.0 diff --git a/tests/test-jex/src/main/java/org/example/Main.java b/tests/test-jex/src/main/java/org/example/Main.java index fc951fcbe..abc569d81 100644 --- a/tests/test-jex/src/main/java/org/example/Main.java +++ b/tests/test-jex/src/main/java/org/example/Main.java @@ -23,7 +23,7 @@ public static Jex.Server start(int port, BeanScope context) { final Jex jex = Jex.create(); jex.routing().addAll(context.list(Routing.HttpService.class)); - jex.routing().error(ValidationException.class, (exception, ctx) -> { + jex.routing().error(ValidationException.class, (ctx, exception) -> { Map map = new LinkedHashMap<>(); map.put("message", exception.getMessage()); map.put("errors", exception.getErrors()); diff --git a/tests/test-jex/src/main/java/org/example/web/TestController.java b/tests/test-jex/src/main/java/org/example/web/TestController.java index 76791b0e0..1f3216767 100644 --- a/tests/test-jex/src/main/java/org/example/web/TestController.java +++ b/tests/test-jex/src/main/java/org/example/web/TestController.java @@ -6,12 +6,13 @@ import io.avaje.http.api.BodyString; import io.avaje.http.api.Controller; import io.avaje.http.api.Default; +import io.avaje.http.api.ExceptionHandler; import io.avaje.http.api.Filter; import io.avaje.http.api.Get; -import io.avaje.http.api.InstrumentServerContext; import io.avaje.http.api.Options; import io.avaje.http.api.Path; import io.avaje.http.api.Post; +import io.avaje.http.api.Produces; import io.avaje.http.api.QueryParam; import io.avaje.jex.Context; import io.avaje.jex.FilterChain; @@ -50,4 +51,21 @@ void filter(FilterChain chain) throws IOException { System.err.println("do nothing lmao"); chain.proceed(); } + + @ExceptionHandler + String exception(IllegalArgumentException ex) { + return "Err: " + ex; + } + + @Produces(statusCode = 501) + @ExceptionHandler + HelloDto exceptionCtx(Exception ex, Context ctx) { + return null; + } + + @ExceptionHandler(IllegalStateException.class) + void exceptionVoid(Context ctx) { + ctx.status(503); + ctx.text("IllegalStateException"); + } }