added root cause support

This commit is contained in:
2018-01-19 23:42:06 +01:00
parent 0c81703042
commit 3e9141d563
2 changed files with 39 additions and 8 deletions

View File

@ -34,29 +34,44 @@ import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
public class APIErrorResponse {
@XmlJavaTypeAdapter(ResponseStatusAdapter.class)
private Response.Status status;
private String errorCode;
private String message;
public APIErrorResponse() {
}
private final Response.Status status;
private final String errorCode;
private final String message;
private final Throwable rootCause;
public APIErrorResponse(APIError apiError, Locale locale) {
this.status = apiError.getStatus();
this.errorCode = apiError.getErrorCode();
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) {
this.status = Response.Status.INTERNAL_SERVER_ERROR;
this.errorCode = "0";
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) {
this.status = status;
this.errorCode = errorCode;
this.message = getLocalizedMessage(messageKey, locale);
this.rootCause = null;
}
public String getErrorCode() {
@ -71,6 +86,10 @@ public class APIErrorResponse {
return this.message;
}
public Throwable getRootCause() {
return rootCause;
}
private String getLocalizedMessage(APIError apiError, Locale locale) {
ResourceBundle resourceBundle = ResourceBundle.getBundle(apiError.getClass().getName(), locale);
return resourceBundle.getString(apiError.getMessageKey());

View File

@ -18,6 +18,7 @@ package de.muehlencord.shared.jeeutil.restexfw;
import java.util.Locale;
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_CODE = "X-Error-Code";
public static final String HTTP_HEADER_X_ROOT_CAUSE = "X-Root-Cause";
private final Response httpResponse;
public APIException(APIError apiError, Locale 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) {
httpResponse = createHttpResponse(new APIErrorResponse(exception, locale));
@ -43,9 +49,15 @@ public class APIException extends RuntimeException {
}
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_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();
}
}