Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

TE-2861: Add better debug info to sentry #4

Open
wants to merge 1 commit into
base: tellu-got
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions lib/http-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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;
Expand Down Expand Up @@ -130,6 +137,7 @@ module.exports = class HttpClient {
logRequestInfo(method, url, headers);

let response;
let errMessage;
let ok = true;

try {
Expand All @@ -140,6 +148,7 @@ module.exports = class HttpClient {
throw error;
}
response = error.response;
errMessage = error?.message;
}

const status = response.statusCode;
Expand All @@ -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
Expand Down