Skip to content

Commit

Permalink
Expose failure reason in ElideResponse
Browse files Browse the repository at this point in the history
- Provide hook for building custom responses in JsonApiEndpoint
  • Loading branch information
Clay Jensen-Reimann committed Aug 8, 2018
1 parent c421a63 commit f4e8f83
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 15 deletions.
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);

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

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) {
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) {
return Response
.status(response.getResponseCode())
.entity(response.getBody())
.build();
}
}

0 comments on commit f4e8f83

Please sign in to comment.