hide stacktrace

This commit is contained in:
Mengyi Zhou 2015-11-09 15:23:15 +08:00
parent 724ed397a7
commit 07aa454cb5
3 changed files with 23 additions and 14 deletions

View file

@ -18,8 +18,8 @@ import javax.ws.rs.core.Response;
public class ErrorResponseHandler implements ResponseHandler {
private static final MediaType defaultMediaType = MediaType.APPLICATION_JSON_TYPE;
public Message generateMessage(Throwable object, String type) throws Exception {
ErrorMessage em = ExceptionUtils.getErrorMessage(object);
public Message generateMessage(Throwable object, String type, boolean printStackTrace) throws Exception {
ErrorMessage em = ExceptionUtils.getErrorMessage(object, printStackTrace);
ErrorResponse err = new ErrorResponse();
if (type.equals(MediaType.APPLICATION_XML)) {
@ -45,14 +45,17 @@ public class ErrorResponseHandler implements ResponseHandler {
@Override
public Response handle(Object object, MediaType mediaType) throws Exception {
return handle(object, mediaType, false);
}
public Response handle(Object object, MediaType mediaType, boolean printStackTrace) throws Exception {
if (object == null || !(object instanceof Throwable)) {
throw new ValidationException("ErrorResponseHandler only accepts Throwable object");
}
if (mediaType == null || !MediaType.APPLICATION_XML_TYPE.equals(mediaType)) {
mediaType = defaultMediaType;
}
Message message = generateMessage((Throwable) object, mediaType.toString());
Message message = generateMessage((Throwable) object, mediaType.toString(), printStackTrace);
return Response.status(message.getStatus()).entity(message.getResponse())
.type(mediaType).build();
}

View file

@ -43,17 +43,23 @@ public class ExceptionAspect implements Ordered{
logger.error(builder.toString());
MediaType mediaType = null;
boolean printStackTrace = false;
for (Object arg : point.getArgs()) {
if (arg instanceof ContainerRequest) {
ContainerRequest cr = (ContainerRequest) arg;
mediaType = cr.getMediaType();
try {
printStackTrace = Boolean.parseBoolean(cr.getHeaderString("slb-stack-trace"));
} catch (Exception ex) {
printStackTrace = false;
}
break;
}
}
if (mediaType == null) {
logger.warn("Request media type cannot be found - use json by default.");
}
return errorResponseHandler.handle(cause, mediaType);
return errorResponseHandler.handle(cause, mediaType, printStackTrace);
} catch (Exception e) {
logger.error("Error response handler doesn't work.");
return null;

View file

@ -22,9 +22,9 @@ public class ExceptionUtils {
return builder.toString();
}
public static ErrorMessage getErrorMessage(Throwable throwable) {
public static ErrorMessage getErrorMessage(Throwable throwable, boolean printStackTrace) {
return new ErrorMessage().setCode(ExceptionUtils.getErrorCode(throwable))
.setMessage(ExceptionUtils.getMessage(throwable))
.setStackTrace(ExceptionUtils.getStackTrace(throwable));
.setStackTrace(printStackTrace ? ExceptionUtils.getStackTrace(throwable) : null);
}
}