Skip to content

Commit

Permalink
Fail fast when VertUtil is invoked incorrectly so we get meaningful s…
Browse files Browse the repository at this point in the history
…tack traces
  • Loading branch information
garricko committed Aug 11, 2017
1 parent d9deb4e commit af6eeb0
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/main/java/com/github/susom/database/VertxUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public class VertxUtil {
* on the thread that calls it.
*/
public static <T> Handler<T> mdc(final Handler<T> handler) {
if (handler == null) {
// Throw here instead of getting NPE inside the handler so we can see the stack trace
throw new IllegalArgumentException("handler may not be null");
}

final Map mdc = MDC.getCopyOfContextMap();

return t -> {
Expand Down Expand Up @@ -58,9 +63,19 @@ public static <T> Handler<T> mdc(final Handler<T> handler) {
* event loop.
*/
public static <T> Handler<T> mdcEventLoop(final Handler<T> handler) {
if (handler == null) {
// Throw here instead of getting NPE inside the handler so we can see the stack trace
throw new IllegalArgumentException("handler may not be null");
}

final Map mdc = MDC.getCopyOfContextMap();
final Context context = Vertx.currentContext();

if (context == null) {
// Throw here instead of getting NPE inside the handler so we can see the stack trace
throw new IllegalStateException("Expecting to be on an Vert.x event loop context");
}

return t -> context.runOnContext((v) -> {
Map restore = MDC.getCopyOfContextMap();
try {
Expand Down

0 comments on commit af6eeb0

Please sign in to comment.