Skip to content
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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
.gradle/
.idea/
.sonar/
bin/
target/
test-output/
.settings
.classpath

.checkstyle
.idea/
.sonar/
.classpath
.settings

*.iml
bin
dependency-reduced-pom.xml
*.jar
*.class
Expand Down
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Change Log

## 4.2.7
**Features**
* Provide failure reason in ElideResponse
* Expose response building in JsonApiEndpoint to allow for customization of response behavior

## 4.2.6
**Fixes**
* Fix NPE serializing Dates
Expand Down
17 changes: 11 additions & 6 deletions elide-core/src/main/java/com/yahoo/elide/Elide.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member

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.


requestScope.runQueuedPreCommitTriggers();
auditLogger.commit(requestScope);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about graphQL?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no real analog to ElideResponse in GraphQL because we're leveraging GraphQLJava. The Elide class drives JsonAPI and is ignored by GraphQL, also the GraphQLEndpiont can't be customized in the way that JsonApiEndpoint can.

}
}

Expand Down
4 changes: 3 additions & 1 deletion elide-core/src/main/java/com/yahoo/elide/ElideResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where does the failureReason get used? I see it being set, but never logged or added to a (presumably verbose?) response.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or is the plan that arbitrary endpoints could leverage this functionality?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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();
}
}