Error handling consistency/conventions
See original GitHub issueI ran into this a while back and never raised it, but since I’m here and contributing to the error handling code (#128 #126 #118) I thought I’d bring it up now.
Errors should be instanceof Error
I would have expected the errors provided in the catch(err)
callback to have been instanceof Error
, as is suggested by the MDN docs for Promise.reject(reason)
:
For debugging purposes and selective error catching, it is useful to make reason an instanceof Error.
Indeed the convention for the continuation-style callback passing cb(err)
is widely recognised, supported and provided by module authors (supporting material: 1,2).
Error handling should be consistent
In debugging and fixing #126, I discovered different error handling strategies for:
The difference being that the error response json is parsed in the RPC request, but not the others. I believe that this inconsistency was introduced by accident rather than design (in the fix to #108).
Solution
I think the solution should be to extract the error construction into a common function that is used by each request type. This will ensure consistency between request type.
The error constructed should be an Error
object, and I see no reason why the information that is attached to the current non-Error
object format could not be attached.
For example (just a quick sketch, not an exhaustive solution), something like:
var buildCustomError = function (error, response) {
var msg;
if (response) {
try {
msg = JSON.parse(response.text).error_summary;
} catch (e) {
msg = error.message;
}
}
var err = new Error(msg);
err.status = error.status;
err.response = response;
return err;
};
Let me know what you think. I’d be happy to put together a PR that implements this solution, or leave it with you to implement. Cheers.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:6 (3 by maintainers)
Top GitHub Comments
would be very nice to see error handling in some /example
We’ve updated the error handling in release v8.0.0, and added a note to the Upgrading document. Please upgrade to the latest version and let us know if you’re still seeing any issues. Thanks!