Created
October 13, 2015 12:47
-
-
Save K0NRAD/081b2c56d974ba946ce8 to your computer and use it in GitHub Desktop.
Java EE ExceptionMapper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.persistence.EntityNotFoundException; | |
import javax.ws.rs.core.Response; | |
import javax.ws.rs.ext.ExceptionMapper; | |
import javax.ws.rs.ext.Provider; | |
@Provider | |
public class EntityNotFoundEjbExceptionMapper implements ExceptionMapper<EntityNotFoundException> { | |
@Override | |
public Response toResponse(EntityNotFoundException exception) { | |
Throwable cause = exception.getCause(); | |
if (cause instanceof EntityNotFoundException) { | |
EntityNotFoundException actual = (EntityNotFoundException) cause; | |
return Response.status(Response.Status.NOT_FOUND). | |
header("cause", "Entity not found."). | |
header("additional-info", actual.getMessage()). | |
build(); | |
} | |
return Response.serverError(). | |
header("cause", exception.toString()).build(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment