diff --git a/api/FHIRApiService/services/base.service.js b/api/FHIRApiService/services/base.service.js index f95ae23f..b155beb1 100644 --- a/api/FHIRApiService/services/base.service.js +++ b/api/FHIRApiService/services/base.service.js @@ -55,7 +55,7 @@ class BaseFhirApiService { code: 409, msg: handleError.duplicate(err.message) }; - } else if (err.stack.includes("ValidationError")) { + } else if (err?.stack?.includes("ValidationError")) { let operationOutcomeError = new OperationOutcome([]); for (let errorKey in err.errors) { @@ -70,7 +70,7 @@ class BaseFhirApiService { msg: operationOutcomeError }; - } else if (err.stack.includes("stored by resource")) { + } else if (err?.stack?.includes("stored by resource")) { operationOutcomeMessage = { code: 400, msg: handleError.processing(err.message) @@ -91,14 +91,16 @@ class BaseFhirApiService { item = handleError.processing(item); } - if (this.response.getHeader("content-type").includes("xml") || - this.request.get("accept").includes("xml") + if ((this.response.getHeader("content-type").includes("xml") || + this.request.get("accept").includes("xml")) || + this.response.locals?._format?.toLowerCase() === "xml" ) { let fhir = new FHIR(); let xmlItem = fhir.objToXml(item); if (this._pretty) xmlItem = xmlFormatter(xmlItem); return this.response.status(code).send(xmlItem); } + return this.response.status(code).send(item); } diff --git a/routes.js b/routes.js index f071c683..b1c32219 100644 --- a/routes.js +++ b/routes.js @@ -12,6 +12,11 @@ if (fs.existsSync(path.join(__dirname, "./plugins/config.js"))) { pluginsConfig = require("./plugins/config.template").pluginsConfig; } +/** + * + * @param {import("express").Request} req + * @param {import("express").Response} res + */ function setFormatWhenQuery(req, res) { let format = _.get(req, "query._format"); if (format && format.includes("xml")) { @@ -19,6 +24,7 @@ function setFormatWhenQuery(req, res) { } else if (format && format.includes("json")) { res.set("Content-Type", "application/fhir+json"); } + res.locals._format = format; delete req["query"]["_format"]; }