Skip to content

Commit

Permalink
Merge pull request #243 from blockfrost/chore/hide-undefined-body
Browse files Browse the repository at this point in the history
chore: hide undefined body prop in BlockfrostServerError
  • Loading branch information
vladimirvolek authored Nov 18, 2022
2 parents 0a46f39 + 683c509 commit e15125e
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions packages/blockfrost-js/src/utils/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,25 +73,33 @@ export const handleError = (
error: GotError,
): BlockfrostServerError | BlockfrostClientError => {
if (error instanceof HTTPError) {
let errorInstance: BlockfrostServerError;
const url = error.request.requestUrl;
const responseBody = error.response.body;

if (isBlockfrostErrorResponse(responseBody)) {
return new BlockfrostServerError({ ...responseBody, url });
errorInstance = new BlockfrostServerError({ ...responseBody, url });
} else {
// response.body may contain html output (eg. errors returned by nginx)
const { statusCode } = error.response;
const statusText = error.response.statusMessage ?? error.message;
return new BlockfrostServerError({
errorInstance = new BlockfrostServerError({
status_code: statusCode,
message: `${statusCode}: ${statusText}`,
error: statusText,
url,
// Sometimes original body can be helpful so let's forward it
// Eg. communicating directly with Cardano Submit API which returns 400 with the error from cardano-node in the body of the request)
...(error.response.body ? { body: error.response.body } : {}),
body: error.response.body ? error.response.body : undefined,
});
}

// remove undefined body prop so it doesn't pollute string representation of the error
if (errorInstance.body === undefined) {
delete errorInstance.body;
}

return errorInstance;
}

// system errors such as -3008 ENOTFOUND and various got errors like ReadError, CacheError, MaxRedirectsError, TimeoutError,...
Expand Down

0 comments on commit e15125e

Please sign in to comment.