added root cause support
This commit is contained in:
@ -34,29 +34,44 @@ import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
|
|||||||
public class APIErrorResponse {
|
public class APIErrorResponse {
|
||||||
|
|
||||||
@XmlJavaTypeAdapter(ResponseStatusAdapter.class)
|
@XmlJavaTypeAdapter(ResponseStatusAdapter.class)
|
||||||
private Response.Status status;
|
private final Response.Status status;
|
||||||
private String errorCode;
|
private final String errorCode;
|
||||||
private String message;
|
private final String message;
|
||||||
|
private final Throwable rootCause;
|
||||||
public APIErrorResponse() {
|
|
||||||
}
|
|
||||||
|
|
||||||
public APIErrorResponse(APIError apiError, Locale locale) {
|
public APIErrorResponse(APIError apiError, Locale locale) {
|
||||||
this.status = apiError.getStatus();
|
this.status = apiError.getStatus();
|
||||||
this.errorCode = apiError.getErrorCode();
|
this.errorCode = apiError.getErrorCode();
|
||||||
this.message = getLocalizedMessage(apiError, locale);
|
this.message = getLocalizedMessage(apiError, locale);
|
||||||
|
this.rootCause = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public APIErrorResponse(APIError apiError, Locale locale, Throwable th) {
|
||||||
|
this.status = apiError.getStatus();
|
||||||
|
this.errorCode = apiError.getErrorCode();
|
||||||
|
this.message = getLocalizedMessage(apiError, locale);
|
||||||
|
this.rootCause = th;
|
||||||
}
|
}
|
||||||
|
|
||||||
public APIErrorResponse(Exception exception, Locale locale) {
|
public APIErrorResponse(Exception exception, Locale locale) {
|
||||||
this.status = Response.Status.INTERNAL_SERVER_ERROR;
|
this.status = Response.Status.INTERNAL_SERVER_ERROR;
|
||||||
this.errorCode = "0";
|
this.errorCode = "0";
|
||||||
this.message = exception.getLocalizedMessage();
|
this.message = exception.getLocalizedMessage();
|
||||||
|
this.rootCause = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public APIErrorResponse(Exception exception, Locale locale, Throwable th) {
|
||||||
|
this.status = Response.Status.INTERNAL_SERVER_ERROR;
|
||||||
|
this.errorCode = "0";
|
||||||
|
this.message = exception.getLocalizedMessage();
|
||||||
|
this.rootCause = th;
|
||||||
}
|
}
|
||||||
|
|
||||||
public APIErrorResponse(Response.Status status, String errorCode, String messageKey, Locale locale) {
|
public APIErrorResponse(Response.Status status, String errorCode, String messageKey, Locale locale) {
|
||||||
this.status = status;
|
this.status = status;
|
||||||
this.errorCode = errorCode;
|
this.errorCode = errorCode;
|
||||||
this.message = getLocalizedMessage(messageKey, locale);
|
this.message = getLocalizedMessage(messageKey, locale);
|
||||||
|
this.rootCause = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getErrorCode() {
|
public String getErrorCode() {
|
||||||
@ -71,6 +86,10 @@ public class APIErrorResponse {
|
|||||||
return this.message;
|
return this.message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Throwable getRootCause() {
|
||||||
|
return rootCause;
|
||||||
|
}
|
||||||
|
|
||||||
private String getLocalizedMessage(APIError apiError, Locale locale) {
|
private String getLocalizedMessage(APIError apiError, Locale locale) {
|
||||||
ResourceBundle resourceBundle = ResourceBundle.getBundle(apiError.getClass().getName(), locale);
|
ResourceBundle resourceBundle = ResourceBundle.getBundle(apiError.getClass().getName(), locale);
|
||||||
return resourceBundle.getString(apiError.getMessageKey());
|
return resourceBundle.getString(apiError.getMessageKey());
|
||||||
|
|||||||
@ -18,6 +18,7 @@ package de.muehlencord.shared.jeeutil.restexfw;
|
|||||||
|
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
|
import javax.ws.rs.core.Response.ResponseBuilder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -27,12 +28,17 @@ public class APIException extends RuntimeException {
|
|||||||
|
|
||||||
public static final String HTTP_HEADER_X_ERROR = "X-Error";
|
public static final String HTTP_HEADER_X_ERROR = "X-Error";
|
||||||
public static final String HTTP_HEADER_X_ERROR_CODE = "X-Error-Code";
|
public static final String HTTP_HEADER_X_ERROR_CODE = "X-Error-Code";
|
||||||
|
public static final String HTTP_HEADER_X_ROOT_CAUSE = "X-Root-Cause";
|
||||||
|
|
||||||
private final Response httpResponse;
|
private final Response httpResponse;
|
||||||
|
|
||||||
public APIException(APIError apiError, Locale locale) {
|
public APIException(APIError apiError, Locale locale) {
|
||||||
httpResponse = createHttpResponse(new APIErrorResponse(apiError, locale));
|
httpResponse = createHttpResponse(new APIErrorResponse(apiError, locale));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public APIException(APIError apiError, Locale locale, Throwable th) {
|
||||||
|
httpResponse = createHttpResponse(new APIErrorResponse(apiError, locale, th));
|
||||||
|
}
|
||||||
|
|
||||||
public APIException(Exception exception, Locale locale) {
|
public APIException(Exception exception, Locale locale) {
|
||||||
httpResponse = createHttpResponse(new APIErrorResponse(exception, locale));
|
httpResponse = createHttpResponse(new APIErrorResponse(exception, locale));
|
||||||
@ -43,9 +49,15 @@ public class APIException extends RuntimeException {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static Response createHttpResponse(APIErrorResponse response) {
|
private static Response createHttpResponse(APIErrorResponse response) {
|
||||||
return Response.status(response.getStatus()).entity(response)
|
ResponseBuilder builder = Response.status(response.getStatus()).entity(response)
|
||||||
.header(HTTP_HEADER_X_ERROR, response.getMessage())
|
.header(HTTP_HEADER_X_ERROR, response.getMessage())
|
||||||
.header(HTTP_HEADER_X_ERROR_CODE, response.getErrorCode()).build();
|
.header(HTTP_HEADER_X_ERROR_CODE, response.getErrorCode());
|
||||||
|
|
||||||
|
if (response.getRootCause() != null) {
|
||||||
|
builder = builder.header(HTTP_HEADER_X_ROOT_CAUSE, builder);
|
||||||
|
}
|
||||||
|
|
||||||
|
return builder.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user