Skip to content

Commit c7d1bcc

Browse files
committed
After handler must reflect errored status code on failure
fix jooby-project#2367
1 parent 5bc4741 commit c7d1bcc

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

jooby/src/main/java/io/jooby/Route.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,8 @@ public interface Handler extends Serializable, Aware {
269269
value = apply(ctx);
270270
} catch (Throwable x) {
271271
cause = x;
272+
// Early mark context as errored response code:
273+
ctx.setResponseCode(ctx.getRouter().errorCode(cause));
272274
}
273275
Object result;
274276
try {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package io.jooby.i2367;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
5+
import io.jooby.AccessLogHandler;
6+
import io.jooby.exception.BadRequestException;
7+
import io.jooby.junit.ServerTest;
8+
import io.jooby.junit.ServerTestRunner;
9+
10+
public class Issue2367 {
11+
12+
@ServerTest
13+
public void shouldAfterHandlerGetTheRightCode(ServerTestRunner runner) {
14+
runner.define(app -> {
15+
app.after((ctx, result, failure) -> {
16+
ctx.send(
17+
"statusCode: " + ctx.getResponseCode().value() + ", result: " + result + ", failure: "
18+
+ failure.getClass().getSimpleName());
19+
});
20+
21+
app.get("/2367", ctx -> {
22+
throw new BadRequestException("intentional error");
23+
});
24+
25+
}).ready(http -> {
26+
http.get("/2367", rsp -> {
27+
assertEquals("statusCode: 400, result: null, failure: BadRequestException", rsp.body().string());
28+
});
29+
});
30+
}
31+
32+
@ServerTest
33+
public void shouldLog404(ServerTestRunner runner) {
34+
runner.define(app -> {
35+
app.decorator(new AccessLogHandler());
36+
37+
app.assets("/2367/*", "/2367");
38+
39+
}).ready(http -> {
40+
http.get("/2367/index.js", rsp -> {
41+
assertEquals(404, rsp.code());
42+
});
43+
});
44+
}
45+
}

0 commit comments

Comments
 (0)