Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

beforeError hook isn't called on 500 responses #575

Open
geoff-harper opened this issue Apr 17, 2024 · 2 comments
Open

beforeError hook isn't called on 500 responses #575

geoff-harper opened this issue Apr 17, 2024 · 2 comments

Comments

@geoff-harper
Copy link

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
Copy link
Owner

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

We have a test to confirm it's working:

ky/test/hooks.ts

Lines 610 to 640 in 2d56baf

test('runs beforeError before throwing HTTPError', async t => {
const server = await createHttpTestServer();
server.post('/', (_request, response) => {
response.status(500).send();
});
await t.throwsAsync(
ky.post(server.url, {
hooks: {
beforeError: [
(error: HTTPError) => {
const {response} = error;
if (response?.body) {
error.name = 'GitHubError';
error.message = `${response.statusText} --- (${response.status})`.trim();
}
return error;
},
],
},
}),
{
name: 'GitHubError',
message: 'Internal Server Error --- (500)',
},
);
await server.close();
});

@geoff-harper
Copy link
Author

geoff-harper commented Apr 19, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants