How to customize the Exception handler?
Use the withExceptionMapper
on UndertowSharafServer
:
val customExceptionMapper: ExceptionMapper = {
case e: MyException =>
val errorPage = MyErrorPage(e.getMessage())
Response.withBody(errorPage)
.withStatus(StatusCode.InternalServerError)
}
val finalExceptionMapper = customExceptionMapper.orElse(ExceptionMapper.default)
val server = UndertowSharafServer(routes)
.withExceptionMapper(finalExceptionMapper)
The ExceptionMapper
is a partial function from an exception to Response
.
Here we need to chain our custom exception mapper before the default one.