Search code examples
vert.x

Vert.X: Rare "Unhandled exception in router" despite errorHandler with statusCode 500


I use errorHandler to log interesting general information like User-ID (if available) and IP. This normally works, but rarely I see "Unhandled exception in router" that hasn't passed by my errorHandler:

ERROR io.vertx.ext.web.RoutingContext - Unhandled exception in router

My errorHandler:

router.errorHandler(500) { ctx ->
// Logging happens.
}

From documentation: 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.

What is missing?


Solution

  • An error handler is different from an exception/failure handler

    router.route().failureHandler(this::handleFailure)
    private fun handleFailure(routingContext: RoutingContext) {
       routingContext.response()
         .putHeader("Content-type", "application/json; charset=utf-8")
         .setStatusCode(500)
         .end(routingContext.failure().message ?: "internal server error")
    }