From 2217b02f1b6d59e622a86258a4ced6186082b177 Mon Sep 17 00:00:00 2001 From: Richard Frostmark Date: Mon, 13 Feb 2023 21:04:48 +0100 Subject: [PATCH] TE-2861: Add better debug info to sentry --- lib/http-client.js | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/http-client.js b/lib/http-client.js index 0070092..59bcf92 100644 --- a/lib/http-client.js +++ b/lib/http-client.js @@ -9,6 +9,15 @@ const defaultHeaders = { accept: 'application/fhir+json' }; const responseAttributeKey = '__response'; const requestAttributeKey = '__request'; +class FhirResponseError extends Error { + constructor(message, response, config) { + super(message); + this.name = "FhirResponseError"; + this.config = config; + this.response = response; + } +} + function agentBuilder() { return { http: new HttpAgent(), @@ -22,18 +31,16 @@ function responseErrorBuilder({ method, headers, url, + errMessage, }) { - const error = { - response: { + const error = new FhirResponseError(errMessage || `FhirResponseError: ${status}`, { status, data, - }, - config: { + },{ method, url, headers, - }, - }; + }); logRequestError(error); return error; @@ -130,6 +137,7 @@ module.exports = class HttpClient { logRequestInfo(method, url, headers); let response; + let errMessage; let ok = true; try { @@ -140,6 +148,7 @@ module.exports = class HttpClient { throw error; } response = error.response; + errMessage = error?.message; } const status = response.statusCode; @@ -151,13 +160,14 @@ module.exports = class HttpClient { if (bodyText) { try { data = JSON.parse(bodyText); - } catch { + } catch(error) { data = bodyText; - throw responseErrorBuilder({ status, data, method, headers, url }); + errMessage = error?.message; + throw responseErrorBuilder({ status, data, method, headers, url, errMessage }); } } if (!ok) { - throw responseErrorBuilder({ status, data, method, headers, url }); + throw responseErrorBuilder({ status, data, method, headers, url, errMessage }); } // Add __response and __request