Include response code in error objects
Open
@Nmargolis is already working on this.
Since Aug 28, 2018.
- Dominant language
- JavaScript
- Stars
- 17
- Forks
- 4
- PR merge metrics
- No merged PRs in 30d
Description
Context
Currently, all of the methods in /lib return new errors with the response body as the error message. As an end user, it would be helpful to have access to the status code, instead of having to parse the response body to try to get to it. For example, we would like to catch 502 errors so we can try those requests later.
For a 502 error, the response body looks like this:
<head><title>502 Bad Gateway</title></head>
<body bgcolor="white">
<center><h1>502 Bad Gateway</h1></center>
</body>
</html>
Proposed change
Currently, errors are returned like this:
request({ ... }, (err, res) => {
if (err) return cb(err);
if (res.statusCode !== 200) return cb(new Error(res.body));
...
});
It would be great if they were returned like this:
request({ ... }, (err, res) => {
if (err) return cb(err);
if (res.statusCode !== 200) {
const error = new Error(res.body);
error.status = res.statusCode;
return cb(error);
}
...
});
cc @ingalls
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.