I have one Jersey(version 1.19) Exception Mapper class to send custom error to rest client.
@Provider
class GenericExceptionMapper extends ExceptionMapper[Exception] {
private val logger = LoggerFactory.getLogger(classOf[GenericExceptionMapper])
override def toResponse(exception: Exception): Response = {
exception match {
case e: IllegalArgumentException => responseWithError(e, 400, () => logger.warn("Illegal argument exception" + e.getMessage))
case e: WebApplicationException => e.getResponse
case e => responseWithError(e, 500, () => logger.error("Exception: " + e.getMessage, e))
}
}
private def responseWithError[T](e: Exception, statusCode: Int, fn: () => Unit) : Response = {
fn()
Response.status(statusCode).`type`(MediaType.TEXT_HTML).entity(e.getMessage).build()
}
}
When I am throwing IllegalArgumentException from Filter class for any mandatory header is missing, the GenericExceptionMapper class is not being invoked. However when any exception is thrown from the resource class(i.e. having @Path annotation), the GenericExceptionMapper is being invoked.
Is there any configuration is missing?
Please note, I have extended PackagesResourceConfig class of Jersey package for package scanning.