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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
18 changes: 11 additions & 7 deletions elide-core/src/main/java/com/yahoo/elide/Elide.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
*/
@Slf4j
public class Elide {

@Getter private final ElideSettings elideSettings;
@Getter private final AuditLogger auditLogger;
@Getter private final DataStore dataStore;
Expand Down Expand Up @@ -212,7 +211,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 +235,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 +275,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
16 changes: 16 additions & 0 deletions elide-core/src/main/java/com/yahoo/elide/ElideResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,37 @@

import lombok.Getter;

import java.util.Optional;

/**
* Elide response object.
*/
public class ElideResponse {
@Getter private final int responseCode;
@Getter private final String body;
@Getter private final Optional<Throwable> failureReason;

/**
* Constructor.
*
* @param responseCode HTTP response code
* @param body returned body string
*/
@Deprecated
public ElideResponse(int responseCode, String body) {
this(responseCode, body, null);
}

/**
* Constructor.
*
* @param responseCode HTTP response code
* @param body returned body string
* @param failureReason the reason the request failed
*/
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 = Optional.ofNullable(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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.google.common.collect.ImmutableMap;
import com.yahoo.elide.Elide;
import com.yahoo.elide.ElideSettings;
import com.yahoo.elide.core.DataStoreTransaction;
Expand Down Expand Up @@ -38,9 +39,7 @@
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.SecurityContext;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.function.Function;
Expand Down Expand Up @@ -70,7 +69,6 @@ public class GraphQLEndpoint {
public GraphQLEndpoint(
@Named("elide") Elide elide,
@Named("elideUserExtractionFunction") DefaultOpaqueUserFunction getUser) {
log.error("Started ~~");
this.elide = elide;
this.elideSettings = elide.getElideSettings();
this.getUser = getUser;
Expand Down Expand Up @@ -179,13 +177,12 @@ private Response executeGraphQLRequest(
requestScope.getPermissionExecutor().executeCommitChecks();
if (query.trim().startsWith(MUTATION)) {
if (!result.getErrors().isEmpty()) {
HashMap<String, Object> abortedResponseObject = new HashMap<String, Object>() {
{
put("errors", result.getErrors());
put("data", null);
}
};
Map<String, Object> abortedResponseObject = ImmutableMap.of(
"errors", result.getErrors(),
"data", null
);
// Do not commit. Throw OK response to process tx.close correctly.
// (default implementations throw an IOException if you leave a dangling SQL transaction)
throw new WebApplicationException(
Response.ok(mapper.writeValueAsString(abortedResponseObject)).build());
}
Expand Down