How to customize the Exception handler?

Use the withExceptionMapper on SharafHandler:

val customExceptionMapper: ExceptionMapper = {
  case e: MyException =>
    val errorPage = MyErrorPage(e.getMessage())
    Response.withBody(errorPage)
        .withStatus(StatusCodes.INTERNAL_SERVER_ERROR)
}
val finalExceptionMapper = customExceptionMapper.orElse(ExceptionMapper.default)
val httpHandler = SharafHandler(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.