Skip to content

Commit 2491a2a

Browse files
committed
Add uncaught error handler
1 parent 4e63f5f commit 2491a2a

File tree

4 files changed

+58
-3
lines changed

4 files changed

+58
-3
lines changed

vertx-web/src/main/java/io/vertx/ext/web/Router.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,18 @@ static Router router(Vertx vertx) {
372372
@Fluent
373373
Router errorHandler(int statusCode, Handler<RoutingContext> errorHandler);
374374

375+
/**
376+
* Specify an handler to handle an error for any status code that doesn't have a specific handler assigned.
377+
* The handler will be called when the context fails and other failure handlers didn't write the reply or when an exception is thrown inside an handler.
378+
* You <b>must not</b> use {@link RoutingContext#next()} inside the error handler
379+
* This does not affect the normal failure routing logic.
380+
*
381+
* @param errorHandler error handler. Note: You <b>must not</b> use {@link RoutingContext#next()} inside the provided handler
382+
* @return a reference to this, so the API can be used fluently
383+
*/
384+
@Fluent
385+
Router uncaughtErrorHandler(Handler<RoutingContext> errorHandler);
386+
375387
/**
376388
* Used to route a context to the router. Used for sub-routers. You wouldn't normally call this method directly.
377389
*

vertx-web/src/main/java/io/vertx/ext/web/impl/RouterImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,12 @@ public synchronized Router errorHandler(int statusCode, Handler<RoutingContext>
296296
return this;
297297
}
298298

299+
@Override
300+
public synchronized Router uncaughtErrorHandler(Handler<RoutingContext> errorHandler) {
301+
state = state.setUncaughtErrorHandler(errorHandler);
302+
return this;
303+
}
304+
299305
synchronized void add(RouteImpl route) {
300306
state = state.addRoute(route);
301307
// notify the listeners as the routes are changed

vertx-web/src/main/java/io/vertx/ext/web/impl/RouterState.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,17 @@ final class RouterState {
5757
private final TreeSet<RouteImpl> routes;
5858
private final int orderSequence;
5959
private final Map<Integer, Handler<RoutingContext>> errorHandlers;
60+
private final Handler<RoutingContext> uncaughtErrorHandler;
6061
private final Handler<Router> modifiedHandler;
6162
private final AllowForwardHeaders allowForward;
6263
private final Map<String, Object> metadata;
6364

64-
public RouterState(RouterImpl router, TreeSet<RouteImpl> routes, int orderSequence, Map<Integer, Handler<RoutingContext>> errorHandlers, Handler<Router> modifiedHandler, AllowForwardHeaders allowForward, Map<String, Object> metadata) {
65+
public RouterState(RouterImpl router, TreeSet<RouteImpl> routes, int orderSequence, Map<Integer, Handler<RoutingContext>> errorHandlers, final Handler<RoutingContext> uncaughtErrorHandler, Handler<Router> modifiedHandler, AllowForwardHeaders allowForward, Map<String, Object> metadata) {
6566
this.router = router;
6667
this.routes = routes;
6768
this.orderSequence = orderSequence;
6869
this.errorHandlers = errorHandlers;
70+
this.uncaughtErrorHandler = uncaughtErrorHandler;
6971
this.modifiedHandler = modifiedHandler;
7072
this.allowForward = allowForward;
7173
this.metadata = metadata;
@@ -78,6 +80,7 @@ public RouterState(RouterImpl router) {
7880
0,
7981
null,
8082
null,
83+
null,
8184
AllowForwardHeaders.NONE,
8285
null);
8386
}
@@ -99,6 +102,7 @@ RouterState setRoutes(Set<RouteImpl> routes) {
99102
new TreeSet<>(routeComparator),
100103
this.orderSequence,
101104
this.errorHandlers,
105+
this.uncaughtErrorHandler,
102106
this.modifiedHandler,
103107
this.allowForward,
104108
this.metadata);
@@ -119,6 +123,7 @@ RouterState addRoute(RouteImpl route) {
119123
routes,
120124
this.orderSequence,
121125
this.errorHandlers,
126+
this.uncaughtErrorHandler,
122127
this.modifiedHandler,
123128
this.allowForward,
124129
this.metadata);
@@ -130,6 +135,7 @@ RouterState clearRoutes() {
130135
new TreeSet<>(routeComparator),
131136
this.orderSequence,
132137
this.errorHandlers,
138+
null,
133139
this.modifiedHandler,
134140
this.allowForward,
135141
this.metadata);
@@ -147,6 +153,7 @@ RouterState removeRoute(RouteImpl route) {
147153
routes,
148154
this.orderSequence,
149155
this.errorHandlers,
156+
this.uncaughtErrorHandler,
150157
this.modifiedHandler,
151158
this.allowForward,
152159
this.metadata);
@@ -162,6 +169,7 @@ RouterState incrementOrderSequence() {
162169
this.routes,
163170
this.orderSequence + 1,
164171
this.errorHandlers,
172+
this.uncaughtErrorHandler,
165173
this.modifiedHandler,
166174
this.allowForward,
167175
this.metadata);
@@ -173,6 +181,7 @@ RouterState setOrderSequence(int orderSequence) {
173181
this.routes,
174182
orderSequence,
175183
this.errorHandlers,
184+
this.uncaughtErrorHandler,
176185
this.modifiedHandler,
177186
this.allowForward,
178187
this.metadata);
@@ -188,16 +197,20 @@ RouterState setErrorHandlers(Map<Integer, Handler<RoutingContext>> errorHandlers
188197
this.routes,
189198
this.orderSequence,
190199
errorHandlers,
200+
this.uncaughtErrorHandler,
191201
this.modifiedHandler,
192202
this.allowForward,
193203
this.metadata);
194204
}
195205

196206
Handler<RoutingContext> getErrorHandler(int errorCode) {
197207
if (errorHandlers != null) {
198-
return errorHandlers.get(errorCode);
208+
final Handler<RoutingContext> errorHandler = errorHandlers.get(errorCode);
209+
if (errorHandler != null) {
210+
return errorHandler;
211+
}
199212
}
200-
return null;
213+
return uncaughtErrorHandler;
201214
}
202215

203216
RouterState putErrorHandler(int errorCode, Handler<RoutingContext> errorHandler) {
@@ -206,6 +219,7 @@ RouterState putErrorHandler(int errorCode, Handler<RoutingContext> errorHandler)
206219
this.routes,
207220
this.orderSequence,
208221
this.errorHandlers == null ? new HashMap<>() : new HashMap<>(errorHandlers),
222+
this.uncaughtErrorHandler,
209223
this.modifiedHandler,
210224
this.allowForward,
211225
this.metadata);
@@ -214,6 +228,18 @@ RouterState putErrorHandler(int errorCode, Handler<RoutingContext> errorHandler)
214228
return newState;
215229
}
216230

231+
RouterState setUncaughtErrorHandler(Handler<RoutingContext> errorHandler) {
232+
return new RouterState(
233+
this.router,
234+
this.routes,
235+
this.orderSequence,
236+
this.errorHandlers,
237+
errorHandler,
238+
this.modifiedHandler,
239+
this.allowForward,
240+
this.metadata);
241+
}
242+
217243
public Handler<Router> getModifiedHandler() {
218244
return modifiedHandler;
219245
}
@@ -224,6 +250,7 @@ public RouterState setModifiedHandler(Handler<Router> modifiedHandler) {
224250
this.routes,
225251
this.orderSequence,
226252
this.errorHandlers,
253+
this.uncaughtErrorHandler,
227254
modifiedHandler,
228255
this.allowForward,
229256
this.metadata);
@@ -235,6 +262,7 @@ public RouterState setAllowForward(AllowForwardHeaders allow) {
235262
this.routes,
236263
this.orderSequence,
237264
this.errorHandlers,
265+
this.uncaughtErrorHandler,
238266
this.modifiedHandler,
239267
allow,
240268
this.metadata);
@@ -256,6 +284,7 @@ public RouterState putMetadata(String key, Object value) {
256284
this.routes,
257285
this.orderSequence,
258286
this.errorHandlers,
287+
this.uncaughtErrorHandler,
259288
this.modifiedHandler,
260289
this.allowForward,
261290
Collections.unmodifiableMap(metadata));

vertx-web/src/test/java/io/vertx/ext/web/RouterTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3645,4 +3645,12 @@ public void testExtractCallee() throws Exception {
36453645
"/?x=1#4",
36463646
200, "OK");
36473647
}
3648+
3649+
@Test
3650+
public void testUncaughtErrorHandler() throws Exception {
3651+
router.route().consumes("text/html").handler(rc -> rc.response().end());
3652+
router.uncaughtErrorHandler(context -> context.response().setStatusCode(context.statusCode()).setStatusMessage("Dumb").end());
3653+
3654+
testRequestWithContentType(HttpMethod.GET, "/foo", "something/html", 415, "Dumb");
3655+
}
36483656
}

0 commit comments

Comments
 (0)