sindresorhus / ky

🌳 Tiny & elegant JavaScript HTTP client based on the browser Fetch API
MIT License
11.83k stars 341 forks source link

beforeError hook isn't called on 500 responses #575

Open geoff-harper opened 2 months ago

geoff-harper commented 2 months ago

Hey I've encountered an issue when receiving 500 server codes from an api. For whatever reason it doesn't seem like the beforeError hook gets called. The following instance isn't producing any logs in the console when I hit a 500.

const api = ky.create({
    retry: 0,
    hooks: {
      beforeError: [
        (error) => {
          console.log('handleBeforeServerError');
        },
      ],
    },
  });
};

I've confirmed that the same code is fine for 404s and other 400 codes. Here are the raw response headers for the 500

HTTP/2 500 
date: Wed, 17 Apr 2024 16:04:41 GMT
content-length: 0
strict-transport-security: max-age=31536000; includeSubDomains
X-Firefox-Spdy: h2

and the 404

HTTP/2 404 
date: Wed, 17 Apr 2024 16:05:52 GMT
content-length: 0
access-control-allow-credentials: true
access-control-allow-origin: http://localhost:3000
vary: Origin
strict-transport-security: max-age=31536000; includeSubDomains
X-Firefox-Spdy: h2
sindresorhus commented 2 months ago

Does 404 work? What environment are you running in? Native Fetch or node-fetch?

We have a test to confirm it's working: https://github.com/sindresorhus/ky/blob/2d56baf6739a49655cbbe74b5083c0350511bbed/test/hooks.ts#L610-L640

geoff-harper commented 2 months ago

It's in native fetch and 404 is working. I can't imagine it would make a difference but I'm noticing it on a PUT req. I've also checked it on Firefox and Chrome. Its options req is 200, I can't remember if it's the same outcome when an options req is 500