Skip to content

Fix incorrect 500 status on bundle POST with only Patients#156

Open
mherman22 wants to merge 1 commit into
intrahealth:masterfrom
mherman22:fix/bundle-post-status-code
Open

Fix incorrect 500 status on bundle POST with only Patients#156
mherman22 wants to merge 1 commit into
intrahealth:masterfrom
mherman22:fix/bundle-post-status-code

Conversation

@mherman22

Copy link
Copy Markdown

Problem

When a FHIR transaction bundle containing only Patient resources is POSTed to /, the server returns HTTP 500 instead of 200.

Root cause in fhir.js line 204:

if(otherResources.entry.length === 0) {
  return callback(null, {});  // returns empty object — no code property
}

Then at line 240:

if(results.patients.code > results.otherResources.code) {
  code = results.patients.code;
} else {
  code = results.otherResources.code;  // undefined
}
if(!code) {
  code = 500;  // defaults to 500
}

200 > undefined is false in JavaScript, so code gets undefined, then defaults to 500.

The symmetric case also has a bug: if the bundle has only non-Patient resources, results.patients is {}, and line 249 (results.patients.responseBundle.entry) crashes with Cannot read properties of undefined.

Fix

Return {code: 200} from the empty otherResources callback, and {code: 200, responseBundle: {entry: []}, responseHeaders: {}, operationSummary: []} from the empty patients callback.

Fixes #68

When a bundle contains only Patient resources (no other resource types),
otherResources.entry is empty and the callback returns {} with no code.
The status code comparison (patients.code > otherResources.code) becomes
(200 > undefined) which is false in JS, so code falls through to
otherResources.code (undefined) and then defaults to 500.

Fix by returning code: 200 from both empty-path callbacks. Also return
a valid responseBundle/responseHeaders/operationSummary from the empty
patients path to prevent a crash on results.patients.responseBundle.entry
when a bundle has only non-Patient resources.

Fixes intrahealth#68
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect 500 status code is returned when a bundle is submitted containing only patient resources

1 participant