Skip to content

Commit 46956db

Browse files
authored
fix jex error controller generation (#518)
1 parent dbf374f commit 46956db

File tree

4 files changed

+31
-9
lines changed

4 files changed

+31
-9
lines changed

http-generator-jex/src/main/java/io/avaje/http/generator/jex/ControllerMethodWriter.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,14 @@ void writeRouting() {
4242
final PathSegments segments = method.pathSegments();
4343
final String fullPath = segments.fullPath();
4444

45-
if (isFilter) {
45+
if (method.isErrorMethod()) {
46+
writer.append(" routing.error(%s.class, this::_%s)", method.exceptionShortName(), method.simpleName());
47+
} else if (isFilter) {
4648
writer.append(" routing.filter(this::_%s)", method.simpleName());
4749
} else {
48-
writer.append(" routing.%s(\"%s\", this::_%s)", webMethod.name().toLowerCase(), fullPath, method.simpleName());
50+
writer.append(
51+
" routing.%s(\"%s\", this::_%s)",
52+
webMethod.name().toLowerCase(), fullPath, method.simpleName());
4953
}
5054

5155
List<String> roles = method.roles();
@@ -65,14 +69,14 @@ void writeRouting() {
6569
void writeHandler(boolean requestScoped) {
6670

6771
if (method.isErrorMethod()) {
68-
writer.append(" private void _%s(Context ctx, %s ex)", method.simpleName(), method.exceptionShortName());
72+
writer.append(" private void _%s(Context ctx, %s ex) {", method.simpleName(), method.exceptionShortName());
6973
} else if (isFilter) {
70-
writer.append(" private void _%s(Context ctx, FilterChain chain)", method.simpleName());
74+
writer.append(" private void _%s(Context ctx, FilterChain chain) throws IOException {", method.simpleName());
7175
} else {
72-
writer.append(" private void _%s(Context ctx)", method.simpleName());
76+
writer.append(" private void _%s(Context ctx) throws IOException {", method.simpleName());
7377
}
7478

75-
writer.append(" throws IOException {", method.simpleName()).eol();
79+
writer.eol();
7680

7781
write(requestScoped);
7882
writer.append(" }").eol().eol();

tests/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<junit.version>5.11.3</junit.version>
1616
<assertj.version>3.26.3</assertj.version>
1717
<jackson.version>2.18.1</jackson.version>
18-
<jex.version>3.0-RC1</jex.version>
18+
<jex.version>3.0-RC2</jex.version>
1919
<avaje-inject.version>11.0</avaje-inject.version>
2020
<nima.version>4.1.4</nima.version>
2121
<javalin.version>6.3.0</javalin.version>

tests/test-jex/src/main/java/org/example/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static Jex.Server start(int port, BeanScope context) {
2323
final Jex jex = Jex.create();
2424
jex.routing().addAll(context.list(Routing.HttpService.class));
2525

26-
jex.routing().error(ValidationException.class, (exception, ctx) -> {
26+
jex.routing().error(ValidationException.class, (ctx, exception) -> {
2727
Map<String, Object> map = new LinkedHashMap<>();
2828
map.put("message", exception.getMessage());
2929
map.put("errors", exception.getErrors());

tests/test-jex/src/main/java/org/example/web/TestController.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
import io.avaje.http.api.BodyString;
77
import io.avaje.http.api.Controller;
88
import io.avaje.http.api.Default;
9+
import io.avaje.http.api.ExceptionHandler;
910
import io.avaje.http.api.Filter;
1011
import io.avaje.http.api.Get;
11-
import io.avaje.http.api.InstrumentServerContext;
1212
import io.avaje.http.api.Options;
1313
import io.avaje.http.api.Path;
1414
import io.avaje.http.api.Post;
15+
import io.avaje.http.api.Produces;
1516
import io.avaje.http.api.QueryParam;
1617
import io.avaje.jex.Context;
1718
import io.avaje.jex.FilterChain;
@@ -50,4 +51,21 @@ void filter(FilterChain chain) throws IOException {
5051
System.err.println("do nothing lmao");
5152
chain.proceed();
5253
}
54+
55+
@ExceptionHandler
56+
String exception(IllegalArgumentException ex) {
57+
return "Err: " + ex;
58+
}
59+
60+
@Produces(statusCode = 501)
61+
@ExceptionHandler
62+
HelloDto exceptionCtx(Exception ex, Context ctx) {
63+
return null;
64+
}
65+
66+
@ExceptionHandler(IllegalStateException.class)
67+
void exceptionVoid(Context ctx) {
68+
ctx.status(503);
69+
ctx.text("IllegalStateException");
70+
}
5371
}

0 commit comments

Comments
 (0)