Because we use a custom format of error we want to customize all error responses ... in particular Forbidden (we succeed) and Unauthorized (we failed).
I read many docs, posts and I tried different solutions you can find in my sample project
First: ServerExceptionMapper
@ServerExceptionMapper
@Priority(1)
fun unauthorized(e: UnauthorizedException) =
mapExceptionIntoError(Response.Status.UNAUTHORIZED, "UNAUTHORIZED", e.message)
Second: ExceptionMapper
@Provider
@Priority(Priorities.AUTHORIZATION)
class UnauthorizedErrorMapper : ExceptionMapper<UnauthorizedException> {
private val logger: Logger = Logger.getLogger(this.javaClass.simpleName)
@Override
override fun toResponse(exception: UnauthorizedException): Response {
logger.severe(exception.message)
val message = exception.message
val code = STATUS.statusCode
val error = ErrorInfo(STATUS.statusCode, STATUS.name, message)
return Response
.status(code)
.entity(error)
.build()
}
companion object {
private val STATUS = Response.Status.UNAUTHORIZED
}
}
Third: failureHandler
@ApplicationScoped
class UnauthorizedExceptionHandler {
fun init(@Observes router: Router) {
router.route().failureHandler { event ->
if (event.failure() is UnauthorizedException) {
event.response().end("CUSTOMIZED_RESPONSE")
} else {
event.next()
}
}
}
}
With no success. What did I miss or do wrong?
Thanks in advance Patrice