Skip to content

Instantly share code, notes, and snippets.

@akandratovich
Last active December 23, 2015 13:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akandratovich/6640237 to your computer and use it in GitHub Desktop.
Save akandratovich/6640237 to your computer and use it in GitHub Desktop.
ExceptionMapper
package com.os.brandsnapper.exception;
public class BrandsnappeException extends RuntimeException {
private final ErrorCode code;
private BrandsnappeException(ErrorCode err, String message) {
super(message == null ? err.getMessage() : message);
code = err;
}
public ErrorCode getCode() {
return code;
}
...
}
package com.os.brandsnapper.exception;
import javax.ws.rs.core.Response;
public enum ErrorCode {
ERR_TIMEOUT ("Execution timeout", Response.Status.INTERNAL_SERVER_ERROR),
ERR_INTERNAL ("Internal server error", Response.Status.INTERNAL_SERVER_ERROR),
ERR_BAD_REQUEST("Bad request", Response.Status.BAD_REQUEST),
ERR_NOT_FOUND ("The requested resource could not be found", Response.Status.NOT_FOUND),
ERR_BQL_INVALID("Error in query string", Response.Status.BAD_REQUEST);
private final String message;
private final Response.Status status;
private ErrorCode(String m, Response.Status s) {
message = m;
status = s;
}
public String getMessage() {
return message;
}
public Response.Status getStatus() {
return status;
}
}
package com.os.brandsnapper.exception;
import static com.os.brandsnapper.exception.BrandsnappeException.*;
import com.google.common.collect.Maps;
import com.os.bransnapper.api.ql.BQLException;
import com.sun.jersey.api.NotFoundException;
import com.sun.jersey.api.ParamException;
import com.sun.jersey.api.ParamException.QueryParamException;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import org.codehaus.jackson.JsonProcessingException;
import org.slf4j.LoggerFactory;
import java.util.Map;
@Provider
public class JsonExceptionMapper implements ExceptionMapper<Exception> {
private org.slf4j.Logger logger = LoggerFactory.getLogger(getClass());
private static BrandsnappeException choose(Exception e) {
if (e instanceof NotFoundException)
return exception(ErrorCode.ERR_NOT_FOUND, e);
if (e instanceof ParamException)
return exception(ErrorCode.ERR_BAD_REQUEST, e, "Invalid parameter specified");
if (e instanceof JsonProcessingException)
return exception(ErrorCode.ERR_BAD_REQUEST, e, "Can not process json data");
else if (e instanceof QueryParamException) {
Throwable c = e.getCause();
if (c == null)
return exception(ErrorCode.ERR_BAD_REQUEST, "Invalid parameters");
if (c instanceof BQLException)
return exception(ErrorCode.ERR_BQL_INVALID, e.getMessage());
}
return exception(ErrorCode.ERR_INTERNAL, e);
}
private static final boolean quiet(Exception e) {
return
(e instanceof WebApplicationException) ||
(e instanceof BrandsnappeException) ||
(e instanceof JsonProcessingException);
}
@Override
public Response toResponse(Exception e) {
boolean quiet = quiet(e);
if (!quiet) logger.warn(null, e);
else logger.warn(String.format("%s :: %s", e.getClass().getName(), e.getMessage()));
BrandsnappeException forward = choose(e);
Map<String, Object> entity = Maps.newHashMap();
entity.put("error", forward.getCode());
entity.put("message", forward.getMessage());
return Response
.status(forward.getCode().getStatus())
.entity(entity)
.type(MediaType.APPLICATION_JSON_TYPE)
.build();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment