mapbox / mapbox/HecateJS

Include response code in error objects

Open
#15 2 comments 0 reactions 1 assignee View on GitHub

@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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.