diff --git a/gxJRS/src/main/java/org/gcube/common/gxrest/response/outbound/GXOutboundErrorResponse.java b/gxJRS/src/main/java/org/gcube/common/gxrest/response/outbound/GXOutboundErrorResponse.java index 6d3a4b9..cb45f87 100644 --- a/gxJRS/src/main/java/org/gcube/common/gxrest/response/outbound/GXOutboundErrorResponse.java +++ b/gxJRS/src/main/java/org/gcube/common/gxrest/response/outbound/GXOutboundErrorResponse.java @@ -1,6 +1,7 @@ package org.gcube.common.gxrest.response.outbound; import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; /** @@ -37,6 +38,16 @@ public class GXOutboundErrorResponse { public static void throwExceptionWithTrace(Exception exception, int keepLines, Response.Status status) { throw new WebStreamException(exception, keepLines, status); } + + /** + * Throws the exception to the client. + * @param exception + * @param keepLines number of lines in the stacktrace to keep (max is 5) + * @param type the media type associated to the response + */ + public static void throwExceptionWithTrace(Exception exception, int keepLines, Response.Status status, MediaType type) { + throw new WebStreamException(exception, keepLines, status, type); + } /** * Throws the exception to the client. diff --git a/gxJRS/src/main/java/org/gcube/common/gxrest/response/outbound/WebStreamException.java b/gxJRS/src/main/java/org/gcube/common/gxrest/response/outbound/WebStreamException.java index f39afa8..f197b5c 100644 --- a/gxJRS/src/main/java/org/gcube/common/gxrest/response/outbound/WebStreamException.java +++ b/gxJRS/src/main/java/org/gcube/common/gxrest/response/outbound/WebStreamException.java @@ -1,6 +1,8 @@ package org.gcube.common.gxrest.response.outbound; +import javax.print.attribute.standard.Media; import javax.ws.rs.WebApplicationException; +import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.gcube.common.gxrest.response.entity.CodeEntity; @@ -19,11 +21,24 @@ final class WebStreamException extends WebApplicationException { /** * Returns the exception. - * - * @param exception - * @param keepLines - * @param status - * the HTTP status to associate to the response. + * + * @param exception the exception + * @param keepLines how many lines of the stacktrace are returned + * @param status the HTTP status to associate to the response. + * @param type the media type associated to the exception + */ + protected WebStreamException(E exception, int keepLines, Response.Status status, MediaType type) { + super(exception.getCause(), Response.status(status) + .entity(new CodeEntity(new SerializableErrorEntity(exception, keepLines))).type(type).tag(EntityTag.gxError).build()); + + } + + /** + * Returns the exception. + * + * @param exception the exception + * @param keepLines how many lines of the stacktrace are returned + * @param status the HTTP status to associate to the response. */ protected WebStreamException(E exception, int keepLines, Response.Status status) { super(exception.getCause(), Response.status(status) @@ -33,15 +48,26 @@ final class WebStreamException extends WebApplicationException { /** * Returns the exception. * - * @param exception - * @param status - * the HTTP status to associate to the response. + * @param exception the exception + * @param keepLines how many lines of the stacktrace are returned + * */ protected WebStreamException(E exception, int keepLines) { this(exception, keepLines, Response.Status.NOT_ACCEPTABLE); } + /** + * Returns the exception. + * + * @param exception the exception + * @param keepLines how many lines of the stacktrace are returned + * + */ + protected WebStreamException(E exception, int keepLines, MediaType type) { + this(exception, keepLines, Response.Status.NOT_ACCEPTABLE, type); + + } /** * Returns the exception. * @@ -61,4 +87,5 @@ final class WebStreamException extends WebApplicationException { protected WebStreamException(E exception, Response.Status status) { this(exception, 0, status); } + }