Skip to content

Commit

Permalink
issue #2535 - wrap exceptions instead of losing them
Browse files Browse the repository at this point in the history
In addition to their OperationOutcomeIssues, we now include the original
exception in the CausedBy of the FHIRRestBundledRequestException.

This way, if the FHIROperationException was unexpected and has no
corresponding issues, when it gets mapped to a 500 Server Error we will
automatically log the exception from
FHIRResource.exceptionResponse(FHIROperationException, Status).

Also changed "bundle request" to "bundled request" in
FHIRRestHelper.logBundleRequestCompletedMsg to match the language at the
start of the bundled request and to make it more clear its the
individual bundled entry that completed and not the entire bundle.

Signed-off-by: Lee Surprenant <lmsurpre@us.ibm.com>
  • Loading branch information
lmsurpre committed Jun 21, 2021
1 parent 3f10951 commit a250314
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,7 @@ protected Response exceptionResponse(FHIRRestBundledRequestException e) {

List<Bundle.Entry> toAdd = new ArrayList<Bundle.Entry>();
// replace bundle entries that have an empty response
for (int i = 0; i < e.getResponseBundle().getEntry().size(); i++) {
Bundle.Entry entry = e.getResponseBundle().getEntry().get(i);
for (Bundle.Entry entry : e.getResponseBundle().getEntry()) {
if (entry.getResponse() != null && entry.getResponse().getStatus() == null) {
entry = entry.toBuilder()
.response(entry.getResponse().toBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1762,7 +1762,7 @@ private List<Entry> processEntriesByMethod(Bundle requestBundle, Map<Integer, En
} catch (FHIRPersistenceResourceNotFoundException e) {
if (failFast) {
String msg = "Error while processing request bundle.";
throw new FHIRRestBundledRequestException(msg).withIssue(e.getIssues());
throw new FHIRRestBundledRequestException(msg, e).withIssue(e.getIssues());
}

responseEntries[entryIndex] = Entry.builder()
Expand All @@ -1771,11 +1771,11 @@ private List<Entry> processEntriesByMethod(Bundle requestBundle, Map<Integer, En
.status(SC_NOT_FOUND_STRING)
.build())
.build();
logBundleRequestCompletedMsg(requestDescription.toString(), initialTime, SC_NOT_FOUND);
logBundledRequestCompletedMsg(requestDescription.toString(), initialTime, SC_NOT_FOUND);
} catch (FHIRPersistenceResourceDeletedException e) {
if (failFast) {
String msg = "Error while processing request bundle.";
throw new FHIRRestBundledRequestException(msg).withIssue(e.getIssues());
throw new FHIRRestBundledRequestException(msg, e).withIssue(e.getIssues());
}

responseEntries[entryIndex] = Entry.builder()
Expand All @@ -1784,11 +1784,11 @@ private List<Entry> processEntriesByMethod(Bundle requestBundle, Map<Integer, En
.status(SC_GONE_STRING)
.build())
.build();
logBundleRequestCompletedMsg(requestDescription.toString(), initialTime, SC_GONE);
logBundledRequestCompletedMsg(requestDescription.toString(), initialTime, SC_GONE);
} catch (FHIROperationException e) {
if (failFast) {
String msg = "Error while processing request bundle.";
throw new FHIRRestBundledRequestException(msg).withIssue(e.getIssues());
throw new FHIRRestBundledRequestException(msg, e).withIssue(e.getIssues());
}

Status status;
Expand All @@ -1804,7 +1804,7 @@ private List<Entry> processEntriesByMethod(Bundle requestBundle, Map<Integer, En
.status(string(Integer.toString(status.getStatusCode())))
.build())
.build();
logBundleRequestCompletedMsg(requestDescription.toString(), initialTime, status.getStatusCode());
logBundledRequestCompletedMsg(requestDescription.toString(), initialTime, status.getStatusCode());
}
} // end foreach method entry
if (log.isLoggable(Level.FINER)) {
Expand Down Expand Up @@ -1968,7 +1968,7 @@ private Entry processEntryForGet(Entry.Request entryRequest, FHIRUrlParser reque
}

// Save the results of the operation in the bundle response field.
logBundleRequestCompletedMsg(requestDescription, initialTime, SC_OK);
logBundledRequestCompletedMsg(requestDescription, initialTime, SC_OK);

return Entry.builder()
.response(Entry.Response.builder()
Expand Down Expand Up @@ -2057,7 +2057,7 @@ private Entry processEntryForPost(Entry requestEntry, Entry validationResponseEn
throw buildRestException(msg, IssueType.NOT_FOUND);
}

logBundleRequestCompletedMsg(requestDescription, initialTime, SC_OK);
logBundledRequestCompletedMsg(requestDescription, initialTime, SC_OK);
return Bundle.Entry.builder()
.resource(result)
.response(Entry.Response.builder()
Expand All @@ -2069,7 +2069,7 @@ private Entry processEntryForPost(Entry requestEntry, Entry validationResponseEn
// This is a 'search' request.
Bundle searchResults = doSearch(pathTokens[0], null, null, queryParams, absoluteUri, null);

logBundleRequestCompletedMsg(requestDescription, initialTime, SC_OK);
logBundledRequestCompletedMsg(requestDescription, initialTime, SC_OK);
return Bundle.Entry.builder()
.resource(searchResults)
.response(Entry.Response.builder()
Expand Down Expand Up @@ -2319,7 +2319,7 @@ private Entry processEntryForDelete(FHIRUrlParser requestURL, String requestDesc
int httpStatus = ior.getStatus().getStatusCode();
OperationOutcome oo = ior.getOperationOutcome();

logBundleRequestCompletedMsg(requestDescription, initialTime, httpStatus);
logBundledRequestCompletedMsg(requestDescription, initialTime, httpStatus);
return Entry.builder()
.response(Entry.Response.builder()
.status(string(Integer.toString(httpStatus)))
Expand Down Expand Up @@ -2579,15 +2579,15 @@ private Entry buildResponseBundleEntry(FHIRRestOperationResponse operationRespon
bundleEntryBuilder.resource(operationResponse.getOperationOutcome());
}

logBundleRequestCompletedMsg(requestDescription, initialTime, httpStatus);
logBundledRequestCompletedMsg(requestDescription, initialTime, httpStatus);
return bundleEntryBuilder.response(entryResponseBuilder.build()).build();
}

private void logBundleRequestCompletedMsg(String requestDescription, long initialTime, int httpStatus) {
private void logBundledRequestCompletedMsg(String requestDescription, long initialTime, int httpStatus) {
StringBuffer statusMsg = new StringBuffer();
statusMsg.append(" status:[" + httpStatus + "]");
double elapsedSecs = (System.currentTimeMillis() - initialTime) / 1000.0;
log.info("Completed bundle request[" + elapsedSecs + " secs]: "
log.info("Completed bundled request[" + elapsedSecs + " secs]: "
+ requestDescription.toString() + statusMsg.toString());
}

Expand Down

0 comments on commit a250314

Please sign in to comment.