Got: Custom errors thrown in afterResponse are suppressed

Created on 9 Jul 2020  Â·  7Comments  Â·  Source: sindresorhus/got

Describe the bug

If my afterResponse hook throws a custom error, I'm not expecting it to be rewritten into a Got Request Error

Actual behavior

Error is rewritten as a RequestError, with no reference to the original error

RequestError: Bad request signature

Expected behavior

Original error is thrown

HmacError: Bad request signature

Code to reproduce

const got = require('got');
const assert = require('assert');

class HmacError extends Error {}

got({
  url: 'https://not-here.com',
  hooks: {
    afterResponse: [
      () => {
        throw new HmacError('Bad request signature');
      },
    ],
  },
}).catch(err => {
  assert.equal(err.name, 'HmacError');
});

This will throw -

(node:73559) UnhandledPromiseRejectionWarning: AssertionError [ERR_ASSERTION]: 'RequestError' == 'HmacError'

Checklist

  • [x] I have read the documentation.
  • [x] I have tried my code with the latest version of Node.js and Got.
enhancement ✭ help wanted ✭

All 7 comments

This behavior is intended as the Got errors provide useful properties for debugging. Instead of overwriting the error, we could write into it.

@szmarczak that makes sense... I'd rather we treat the errors as immutable (otherwise you can end up in a right old mess with Typescript!)

How about we keep a reference to the originalError within the RequestError?

Just also noticed from the error that only the timings are attached to the error - how can I access the underlying request or response when my afterResponse handler throws an error? Might be worth attaching request and response wherever possible too (potentially non-enumerable), I'd like to see what failed request was rather than just the timings

Actually if it's missing request and response properties at this point then it's a bug... It should have that already.

You're right, sorry about the last comment, didn't even check - it's there but non-enumerable already so didn't show up in my console.log

It's non-enumerable so if you use Sentry or any other error-catching API you won't get bloated data. Although I think we could fix that. See #1067

Closing in favor of #1353

Was this page helpful?
0 / 5 - 0 ratings

Related issues

darksabrefr picture darksabrefr  Â·  3Comments

lukehorvat picture lukehorvat  Â·  3Comments

carvallegro picture carvallegro  Â·  4Comments

jamestalmage picture jamestalmage  Â·  3Comments

pvdlg picture pvdlg  Â·  3Comments