-
Notifications
You must be signed in to change notification settings - Fork 228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose failure reason in ElideResponse #690
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -212,7 +212,7 @@ protected ElideResponse handleRequest(boolean isReadOnly, Object opaqueUser, | |
} | ||
tx.flush(requestScope); | ||
|
||
ElideResponse response = buildResponse(responder.get()); | ||
ElideResponse response = buildResponse(responder.get(), null); | ||
|
||
requestScope.runQueuedPreCommitTriggers(); | ||
auditLogger.commit(requestScope); | ||
|
@@ -236,7 +236,7 @@ protected ElideResponse handleRequest(boolean isReadOnly, Object opaqueUser, | |
|
||
} catch (JsonPatchExtensionException e) { | ||
log.debug("JSON patch extension exception caught", e); | ||
return buildResponse(e.getResponse()); | ||
return buildResponse(e.getResponse(), e); | ||
|
||
} catch (HttpStatusException e) { | ||
log.debug("Caught HTTP status exception", e); | ||
|
@@ -276,19 +276,24 @@ protected ElideResponse buildErrorResponse(HttpStatusException error, boolean is | |
ErrorObjects errors = ErrorObjects.builder().addError() | ||
.withDetail(isVerbose ? error.getVerboseMessage() : error.toString()).build(); | ||
JsonNode responseBody = mapper.getObjectMapper().convertValue(errors, JsonNode.class); | ||
return buildResponse(Pair.of(error.getStatus(), responseBody)); | ||
return buildResponse(Pair.of(error.getStatus(), responseBody), error); | ||
} | ||
return buildResponse(isVerbose ? error.getVerboseErrorResponse() : error.getErrorResponse()); | ||
return buildResponse(isVerbose ? error.getVerboseErrorResponse() : error.getErrorResponse(), error); | ||
} | ||
|
||
@Deprecated | ||
protected ElideResponse buildResponse(Pair<Integer, JsonNode> response) { | ||
return buildResponse(response, null); | ||
} | ||
|
||
protected ElideResponse buildResponse(Pair<Integer, JsonNode> response, Throwable failureReason) { | ||
try { | ||
JsonNode responseNode = response.getRight(); | ||
Integer responseCode = response.getLeft(); | ||
String body = responseNode == null ? null : mapper.writeJsonApiDocument(responseNode); | ||
return new ElideResponse(responseCode, body); | ||
return new ElideResponse(responseCode, body, failureReason); | ||
} catch (JsonProcessingException e) { | ||
return new ElideResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.toString()); | ||
return new ElideResponse(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.toString(), e); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about graphQL? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no real analog to |
||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,15 +13,17 @@ | |
public class ElideResponse { | ||
@Getter private final int responseCode; | ||
@Getter private final String body; | ||
@Getter private final Throwable failureReason; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param responseCode HTTP response code | ||
* @param body returned body string | ||
*/ | ||
public ElideResponse(int responseCode, String body) { | ||
public ElideResponse(int responseCode, String body, Throwable failureReason) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would keep the original public constructor too and deprecate it. |
||
this.responseCode = responseCode; | ||
this.body = body; | ||
this.failureReason = failureReason; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,7 +42,7 @@ public class JsonApiEndpoint { | |
public JsonApiEndpoint(@Named("elide") Elide elide, | ||
@Named("elideUserExtractionFunction") DefaultOpaqueUserFunction getUser) { | ||
this.elide = elide; | ||
this.getUser = getUser == null ? v -> null : getUser; | ||
this.getUser = getUser == null ? ctx -> null : getUser; | ||
} | ||
|
||
/** | ||
|
@@ -121,7 +121,10 @@ public Response delete( | |
return build(elide.delete(path, jsonApiDocument, getUser.apply(securityContext))); | ||
} | ||
|
||
private static Response build(ElideResponse response) { | ||
return Response.status(response.getResponseCode()).entity(response.getBody()).build(); | ||
protected Response build(ElideResponse response) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where does the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or is the plan that arbitrary endpoints could leverage this functionality? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We'll be using it internally. Right now error cause is opaque to consumers of Elide unless you override a bunch of stuff. |
||
return Response | ||
.status(response.getResponseCode()) | ||
.entity(response.getBody()) | ||
.build(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe use an Optional here.