chore(fetcher): add response header info to non-expected errors

This commit is contained in:
WebStreamr 2025-06-01 13:19:38 +00:00
parent ca981220c9
commit 717019e04e
No known key found for this signature in database
2 changed files with 7 additions and 4 deletions

View file

@ -73,10 +73,11 @@ describe('fetch', () => {
await expect(fetcher.text(ctx, new URL('https://some-cloudflare-url.test/'))).rejects.toBeInstanceOf(CloudflareChallengeError);
});
test('passes through other errors', async () => {
fetchMock.get('https://some-error-url.test/', 500);
test('passes through other errors with detailed infos', async () => {
fetchMock.get('https://some-error-url.test/', { status: 500, headers: { 'x-foo': 'bar' } });
await expect(fetcher.text(ctx, new URL('https://some-error-url.test/'))).rejects.toBeInstanceOf(Error);
await expect(fetcher.text(ctx, new URL('https://some-error-url.test/'))).rejects
.toThrow('Fetcher error: 500: Internal Server Error, response headers: {"content-length":"0","x-foo":"bar","age":"0","date":"Wed, 11 Apr 1990 12:34:56 GMT"}');
});
test('passes through exceptions', async () => {

View file

@ -52,11 +52,13 @@ export class Fetcher {
throw new NotFoundError();
}
const responseHeaders = httpCacheItem.policy.responseHeaders();
if (httpCacheItem.policy.responseHeaders()['cf-mitigated'] === 'challenge') {
throw new CloudflareChallengeError();
}
throw new Error(`Fetcher error: ${httpCacheItem.status}: ${httpCacheItem.statusText}`);
throw new Error(`Fetcher error: ${httpCacheItem.status}: ${httpCacheItem.statusText}, response headers: ${JSON.stringify(responseHeaders)}`);
};
private readonly headersToObject = (headers: Headers): Record<string, string> => {