From d78dca3345510b3e30b5d743b78386be803da9ce Mon Sep 17 00:00:00 2001 From: WebStreamr <210764791+webstreamr@users.noreply.github.com> Date: Wed, 4 Jun 2025 08:45:08 +0000 Subject: [PATCH] chore: log response headers for blocks --- src/error/BlockedError.ts | 4 +++- src/utils/Fetcher.ts | 4 ++-- src/utils/StreamResolver.test.ts | 2 +- src/utils/StreamResolver.ts | 2 +- 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/error/BlockedError.ts b/src/error/BlockedError.ts index efb0eed..7722bfb 100644 --- a/src/error/BlockedError.ts +++ b/src/error/BlockedError.ts @@ -2,10 +2,12 @@ import { BlockedReason } from '../types'; export class BlockedError extends Error { public readonly reason: BlockedReason; + public readonly headers: Record; - constructor(reason: BlockedReason) { + constructor(reason: BlockedReason, headers: Record) { super(); this.reason = reason; + this.headers = headers; } } diff --git a/src/utils/Fetcher.ts b/src/utils/Fetcher.ts index c78e0fa..5082d0a 100644 --- a/src/utils/Fetcher.ts +++ b/src/utils/Fetcher.ts @@ -73,11 +73,11 @@ export class Fetcher { const responseHeaders = httpCacheItem.policy.responseHeaders(); if (httpCacheItem.policy.responseHeaders()['cf-mitigated'] === 'challenge') { - throw new BlockedError('cloudflare_challenge'); + throw new BlockedError('cloudflare_challenge', httpCacheItem.policy.responseHeaders()); } if (httpCacheItem.status === 403) { - throw new BlockedError('unknown'); + throw new BlockedError('unknown', httpCacheItem.policy.responseHeaders()); } throw new Error(`Fetcher error: ${httpCacheItem.status}: ${httpCacheItem.statusText}, response headers: ${JSON.stringify(responseHeaders)}`); diff --git a/src/utils/StreamResolver.test.ts b/src/utils/StreamResolver.test.ts index 12a6f3a..940fcb2 100644 --- a/src/utils/StreamResolver.test.ts +++ b/src/utils/StreamResolver.test.ts @@ -72,7 +72,7 @@ describe('resolve', () => { { url: new URL('https://example.com'), isExternal: true, - error: new BlockedError('cloudflare_challenge'), + error: new BlockedError('cloudflare_challenge', {}), label: 'hoster.com', sourceId: '', meta: { diff --git a/src/utils/StreamResolver.ts b/src/utils/StreamResolver.ts index 6f0f553..1896226 100644 --- a/src/utils/StreamResolver.ts +++ b/src/utils/StreamResolver.ts @@ -114,7 +114,7 @@ export class StreamResolver { private readonly logErrorAndReturnNiceString = (ctx: Context, source: string, error: unknown): string => { if (error instanceof BlockedError) { - this.logger.warn(`${source}: Request was blocked. Reason: ${error.reason}`, ctx); + this.logger.warn(`${source}: Request was blocked. Reason: ${error.reason}, headers: ${JSON.stringify(error.headers)}`, ctx); return '⚠️ Request was blocked.'; }