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
Reorganized Router error handling #1140 #1142
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -363,10 +363,24 @@ default void accept(HttpServerRequest request) { | |
* | ||
* @param exceptionHandler the exception handler | ||
* @return a reference to this, so the API can be used fluently | ||
* @deprecated you should use {@link Router#errorHandler(int, Handler)} with 500 status code | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we want to remove that in Vert.x 4 ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes this is the idea. Maybe we can put the new method in 3.7 and remove the old one in 4 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And of course I will reflect these changes in vertx-web-api-contract too |
||
*/ | ||
@Deprecated | ||
@Fluent | ||
Router exceptionHandler(@Nullable Handler<Throwable> exceptionHandler); | ||
|
||
/** | ||
* Specify an handler to handle an error for a particular status code. You can use to manage general errors too using status code 500. | ||
* 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. | ||
* You <b>must not</b> use {@link RoutingContext#next()} inside the error handler | ||
* This does not affect the normal failure routing logic. | ||
* | ||
* @param statusCode status code the errorHandler is capable of handle | ||
* @param errorHandler error handler. Note: You <b>must not</b> use {@link RoutingContext#next()} inside the provided handler | ||
* @return a reference to this, so the API can be used fluently | ||
*/ | ||
Router errorHandler(int statusCode, Handler<RoutingContext> errorHandler); | ||
pmlopes marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Used to route a context to the router. Used for sub-routers. You wouldn't normally call this method directly. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ | |
|
||
package io.vertx.ext.web.impl; | ||
|
||
import io.netty.handler.codec.http.HttpHeaderNames; | ||
import io.vertx.core.Handler; | ||
import io.vertx.core.Vertx; | ||
import io.vertx.core.http.HttpMethod; | ||
|
@@ -27,6 +28,7 @@ | |
import io.vertx.ext.web.RoutingContext; | ||
|
||
import java.util.*; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentSkipListSet; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
|
@@ -59,7 +61,6 @@ public class RouterImpl implements Router { | |
return compare; | ||
}; | ||
|
||
|
||
private static final Logger log = LoggerFactory.getLogger(RouterImpl.class); | ||
|
||
private final Vertx vertx; | ||
|
@@ -70,7 +71,7 @@ public RouterImpl(Vertx vertx) { | |
} | ||
|
||
private final AtomicInteger orderSequence = new AtomicInteger(); | ||
private Handler<Throwable> exceptionHandler; | ||
private Map<Integer, Handler<RoutingContext>> errorHandlers = new ConcurrentHashMap<>(); | ||
|
||
@Override | ||
public void handle(HttpServerRequest request) { | ||
|
@@ -272,9 +273,19 @@ public Router mountSubRouter(String mountPoint, Router subRouter) { | |
return this; | ||
} | ||
|
||
@Deprecated | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need to deprecate the implementation when the base method is deprecated |
||
@Override | ||
public synchronized Router exceptionHandler(Handler<Throwable> exceptionHandler) { | ||
this.exceptionHandler = exceptionHandler; | ||
if (exceptionHandler != null) { | ||
this.errorHandler(500, routingContext -> exceptionHandler.handle(routingContext.failure())); | ||
} | ||
return this; | ||
} | ||
|
||
@Override | ||
public Router errorHandler(int statusCode, Handler<RoutingContext> errorHandler) { | ||
Objects.requireNonNull(errorHandler); | ||
this.errorHandlers.put(statusCode, errorHandler); | ||
return this; | ||
} | ||
|
||
|
@@ -294,8 +305,8 @@ Iterator<RouteImpl> iterator() { | |
return routes.iterator(); | ||
} | ||
|
||
Handler<Throwable> exceptionHandler() { | ||
return exceptionHandler; | ||
Handler<RoutingContext> getErrorHandlerByStatusCode(int statusCode) { | ||
return errorHandlers.get(statusCode); | ||
} | ||
|
||
private String getAndCheckRoutePath(RoutingContext ctx) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No need for "Anyway", simply "You can provide..."