Skip to content

[jex-generator] Fix jex error controller generation #518

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> roles = method.roles();
Expand All @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<junit.version>5.11.3</junit.version>
<assertj.version>3.26.3</assertj.version>
<jackson.version>2.18.1</jackson.version>
<jex.version>3.0-RC1</jex.version>
<jex.version>3.0-RC2</jex.version>
<avaje-inject.version>11.0</avaje-inject.version>
<nima.version>4.1.4</nima.version>
<javalin.version>6.3.0</javalin.version>
Expand Down
2 changes: 1 addition & 1 deletion tests/test-jex/src/main/java/org/example/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> map = new LinkedHashMap<>();
map.put("message", exception.getMessage());
map.put("errors", exception.getErrors());
Expand Down
20 changes: 19 additions & 1 deletion tests/test-jex/src/main/java/org/example/web/TestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}
}