Got: Throw helpful error messages

Created on 20 Mar 2020  Â·  7Comments  Â·  Source: sindresorhus/got

What problem are you trying to solve?

I'm using Github API. On error (non-200 http status) got throws an HTTPError which is by default printed like this:

HTTPError: Response code 422 (Unprocessable Entity)
    at EventEmitter.<anonymous> (.../node_modules/got/dist/source/as-promise.js:118:31)
    at processTicksAndRejections (internal/process/task_queues.js:97:5) {
  name: 'HTTPError'
}

It gives me no information from the response body to be able to debug the issue (e.g on the local machine, or from the server logs in production).

Describe the feature

What I'd like to see is that it'd print the response.body, e.g by including it in Error.message (maybe limiting the maximum length, for sanity). Now I need to add this code to achieve the needed result:

await got(...)
  .catch(err => {
    console.log((err as HTTPError).response.body)
  })

Then it prints like this:

{"message":"Reference already exists","documentation_url":"https://developer.github.com/v3/git/refs/#create-a-reference"}

And suddenly the error message becomes very useful.

I'm aware that I can use Hooks, extend my got instance with some error-handling hook to achieve that (that what I'm going to try now). But wouldn't everyone benefit from such feature enabled by default in got? Or behind a non-default configuration flag?
...

Checklist

  • [x] I have read the documentation and made sure this feature doesn't already exist.
enhancement ✭ help wanted ✭

Most helpful comment

got.HTTPError has a lot of useful properties within request and response, but they are non-enumerable for some reason. This means that they don't get printed by console.error() (which uses util.inspect() under the hood). The only property that is enumerable is timings, which is probably the least useful property out of all of them. This makes the log output of Got errors very verbose, with providing almost no useful information in that output (very bad signal/noise ratio):

HTTPError: Response code 403 (Forbidden)
    at Request.<anonymous> (node_modules/got/dist/source/as-promise/index.js:117:42)
    at processTicksAndRejections (internal/process/task_queues.js:93:5) {
  code: undefined,
  timings: {
    start: 1613304139851,
    socket: 1613304139851,
    lookup: 1613304139851,
    connect: 1613304139917,
    secureConnect: 1613304139986,
    upload: 1613304139986,
    response: 1613304140680,
    end: 1613304140681,
    error: undefined,
    abort: undefined,
    phases: {
      wait: 0,
      dns: 0,
      tcp: 66,
      tls: 69,
      request: 0,
      firstByte: 694,
      download: 1,
      total: 830
    }
  }
}

All 7 comments

https://github.com/sindresorhus/got/blob/master/readme.md#errors It's your responsibility to catch the error and read the useful information. Options aren't logged on purpose to prevent the leak of tokens.

@sindresorhus I think each error message should contain the request method and its URL.

Agreed

FWIW, I had the same problem as @kirillgroshkov today and would have found the proposed functionality useful and intuitive

Encountered this today. Error is thrown and no useful information can be derived from the error thrown. Not even the body returned from the request.

I agree too. I'm starting to use the lib and that was my first unconfortable usecase. Would be awesome to receive formated erros by default.

The problem is we can't identify what kind of actual errors are really sent from the API, for example, 400 Bad Request could mean an invalid token but it could be mean something else too.

got.HTTPError has a lot of useful properties within request and response, but they are non-enumerable for some reason. This means that they don't get printed by console.error() (which uses util.inspect() under the hood). The only property that is enumerable is timings, which is probably the least useful property out of all of them. This makes the log output of Got errors very verbose, with providing almost no useful information in that output (very bad signal/noise ratio):

HTTPError: Response code 403 (Forbidden)
    at Request.<anonymous> (node_modules/got/dist/source/as-promise/index.js:117:42)
    at processTicksAndRejections (internal/process/task_queues.js:93:5) {
  code: undefined,
  timings: {
    start: 1613304139851,
    socket: 1613304139851,
    lookup: 1613304139851,
    connect: 1613304139917,
    secureConnect: 1613304139986,
    upload: 1613304139986,
    response: 1613304140680,
    end: 1613304140681,
    error: undefined,
    abort: undefined,
    phases: {
      wait: 0,
      dns: 0,
      tcp: 66,
      tls: 69,
      request: 0,
      firstByte: 694,
      download: 1,
      total: 830
    }
  }
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

lukehorvat picture lukehorvat  Â·  3Comments

framerate picture framerate  Â·  4Comments

sindresorhus picture sindresorhus  Â·  3Comments

astoilkov picture astoilkov  Â·  3Comments

AxelTerizaki picture AxelTerizaki  Â·  3Comments