Skip to content

Commit

Permalink
Throw client errors correctly. (#293)
Browse files Browse the repository at this point in the history
If the HTTP client catches an error but it doesn't have a response, then it is not an HTTP error, so we'll throw the original error instead. This will surface what is causing an error that isn't because of an HTTP response in the client.

Should fix #229, or at least help diagnose it.
  • Loading branch information
philnash authored Mar 13, 2023
1 parent b351812 commit c95c089
Showing 1 changed file with 19 additions and 14 deletions.
33 changes: 19 additions & 14 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,26 @@ class NgrokClient {
}
} catch (error) {
let clientError;
try {
const response = JSON.parse(error.response.body);
clientError = new NgrokClientError(
response.msg,
error.response,
response
);
} catch (e) {
clientError = new NgrokClientError(
error.response.body,
error.response,
error.response.body
);
if (error.response) {
try {
const response = JSON.parse(error.response.body);
clientError = new NgrokClientError(
response.msg,
error.response,
response
);
} catch (e) {
clientError = new NgrokClientError(
error.response.body,
error.response,
error.response.body
);
}
throw clientError;
} else {
// Rethrow the original error as it is not an HTTP error.
throw error;
}
throw clientError;
}
}

Expand Down

0 comments on commit c95c089

Please sign in to comment.