How about like what Christian Gürtler answered but with a slight adjustment, instead of using the base exception in the arrow brackets, one can use a custom exception class that extends the base exception class and then one can define all custom exceptions to extend from the custom exception class, this approach should allow you to have generic handlers that do not override handlers for the base Exception class:
@Provider
public class CustomExceptionHandler implements ExceptionMapper<CustomException> {
@Override
public Response toResponse(CustomException exception) {
return Response.status(exception.getErrorType().getHttpStatusCode()).entity(exception.getErrorType().getReason()).build();
}
}
public class CustomException extends Exception {
private final CustomError type;
private Exception innerException;
public CustomException(CustomError type) {
super(type.getReason());
this.type = type;
}
public CustomException(CustomError type, Exception e) {
super(type.getReason()+","+e.getMessage());
this.type = type;
this.innerException = e;
}
public CustomException(CustomError type, String message) {
super(type.getReason()+","+message);
this.type = type;
}
public CustomException(CustomError type, String message, Exception e) {
super(type.getReason()+","+message,e);
this.type = type;
this.innerException = e;
}
public CustomError getErrorType() {
return type;
}
}
public class CustomError {
private String errorCode;
private String reason;
private int httpStatusCode;
public CustomError(String errorCode, String reason,
int httpStatusCode) {
this.errorCode = errorCode;
this.reason = reason;
this.httpStatusCode = httpStatusCode;
}
public String getErrorCode() {
return this.errorCode;
}
public void setErrorCode(String errorCode) {
this.errorCode = errorCode;
}
public String getReason() {
return this.reason;
}
public void setReason(String reason) {
this.reason = reason;
}
public void setHttpStatusCode(int httpStatusCode) {
this.httpStatusCode = httpStatusCode;
}
public int getHttpStatusCode() {
return this.httpStatusCode;
}
}