Skip to content

Commit

Permalink
Merge pull request yahoo#482 from yahoo/verbose_handling
Browse files Browse the repository at this point in the history
Properly handle exceptions in handler
  • Loading branch information
Dennis McWherter authored Jun 28, 2017
2 parents ceffd93 + 5742bdb commit ee964ea
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
37 changes: 25 additions & 12 deletions elide-core/src/main/java/com/yahoo/elide/Elide.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,12 @@ public ElideResponse get(String path, MultivaluedMap<String, String> queryParams
JsonApiDocument jsonApiDoc = new JsonApiDocument();
RequestScope requestScope = new RequestScope(path, jsonApiDoc, tx, user, queryParams, elideSettings);
BaseVisitor visitor = new GetVisitor(requestScope);
Supplier<Pair<Integer, JsonNode>> responder = visitor.visit(parse(path));

return new HandlerResult(requestScope, responder);
try {
Supplier<Pair<Integer, JsonNode>> responder = visitor.visit(parse(path));
return new HandlerResult(requestScope, responder);
} catch (RuntimeException e) {
return new HandlerResult(requestScope, e);
}
});

}
Expand All @@ -106,9 +109,12 @@ public ElideResponse post(String path, String jsonApiDocument, Object opaqueUser
JsonApiDocument jsonApiDoc = mapper.readJsonApiDocument(jsonApiDocument);
RequestScope requestScope = new RequestScope(path, jsonApiDoc, tx, user, null, elideSettings);
BaseVisitor visitor = new PostVisitor(requestScope);
Supplier<Pair<Integer, JsonNode>> responder = visitor.visit(parse(path));

return new HandlerResult(requestScope, responder);
try {
Supplier<Pair<Integer, JsonNode>> responder = visitor.visit(parse(path));
return new HandlerResult(requestScope, responder);
} catch (RuntimeException e) {
return new HandlerResult(requestScope, e);
}
});
}

Expand All @@ -129,18 +135,25 @@ public ElideResponse patch(String contentType, String accept,
if (JsonApiPatch.isPatchExtension(contentType) && JsonApiPatch.isPatchExtension(accept)) {
handler = (tx, user) -> {
PatchRequestScope requestScope = new PatchRequestScope(path, tx, user, elideSettings);
Supplier<Pair<Integer, JsonNode>> responder =
JsonApiPatch.processJsonPatch(dataStore, path, jsonApiDocument, requestScope);
return new HandlerResult(requestScope, responder);
try {
Supplier<Pair<Integer, JsonNode>> responder =
JsonApiPatch.processJsonPatch(dataStore, path, jsonApiDocument, requestScope);
return new HandlerResult(requestScope, responder);
} catch (RuntimeException e) {
return new HandlerResult(requestScope, e);
}
};
} else {
handler = (tx, user) -> {
JsonApiDocument jsonApiDoc = mapper.readJsonApiDocument(jsonApiDocument);
RequestScope requestScope = new RequestScope(path, jsonApiDoc, tx, user, null, elideSettings);
BaseVisitor visitor = new PatchVisitor(requestScope);
Supplier<Pair<Integer, JsonNode>> responder = visitor.visit(parse(path));

return new HandlerResult(requestScope, responder);
try {
Supplier<Pair<Integer, JsonNode>> responder = visitor.visit(parse(path));
return new HandlerResult(requestScope, responder);
} catch (RuntimeException e) {
return new HandlerResult(requestScope, e);
}
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,17 @@ public void postProcess(PatchRequestScope requestScope) {
* @param patchDoc the patch doc
* @param requestScope request scope
* @return pair
* @throws IOException the iO exception
*/
public static Supplier<Pair<Integer, JsonNode>> processJsonPatch(DataStore dataStore,
String uri,
String patchDoc,
PatchRequestScope requestScope)
throws IOException {
List<Patch> actions = requestScope.getMapper().readJsonApiPatchExtDoc(patchDoc);
PatchRequestScope requestScope) {
List<Patch> actions;
try {
actions = requestScope.getMapper().readJsonApiPatchExtDoc(patchDoc);
} catch (IOException e) {
throw new InvalidEntityBodyException(patchDoc);
}
JsonApiPatch processor = new JsonApiPatch(dataStore, actions, uri, requestScope);
return processor.processActions(requestScope);
}
Expand Down

0 comments on commit ee964ea

Please sign in to comment.