Skip to content

Commit

Permalink
🔊 better logging of JSON errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ianardee committed Aug 11, 2023
1 parent 723e208 commit 487158b
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/api/endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,20 +176,28 @@ export class Endpoint {

let responseBody = "";
res.on("data", function (chunk: string) {
logger.debug("receiving data ...");
logger.debug("Receiving data ...");
responseBody += chunk;
});
res.on("end", function () {
logger.debug("Parsing the response ...");
// handle empty responses from server, for example in the case of redirects
if (!responseBody) {
responseBody = "{}";
}
try {
resolve({
messageObj: res,
data: JSON.parse(responseBody),
});
const parsedResponse = JSON.parse(responseBody);
try {
resolve({
messageObj: res,
data: parsedResponse,
});
} catch (error) {
logger.error("Could not construct the return object.");
reject(error);
}
} catch (error) {
logger.error("Could not parse the return as JSON.");
logger.debug(responseBody);
reject(error);
}
Expand Down

0 comments on commit 487158b

Please sign in to comment.