Add to the error response a method that allows to set the media type for the returned entity.

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/Common/gxREST@178713 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Manuele Simi 2019-03-28 03:22:02 +00:00
parent 7993999be2
commit 575c3daa75
2 changed files with 46 additions and 8 deletions

View File

@ -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.

View File

@ -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 <E extends Exception> 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 <E extends Exception> 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 <E extends Exception> 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 <E extends Exception> 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 <E extends Exception> WebStreamException(E exception, Response.Status status) {
this(exception, 0, status);
}
}