chore: log response headers for blocks

This commit is contained in:
WebStreamr 2025-06-04 08:45:08 +00:00
parent c5cc955d91
commit d78dca3345
No known key found for this signature in database
4 changed files with 7 additions and 5 deletions

View file

@ -2,10 +2,12 @@ import { BlockedReason } from '../types';
export class BlockedError extends Error {
public readonly reason: BlockedReason;
public readonly headers: Record<string, string[] | string | undefined>;
constructor(reason: BlockedReason) {
constructor(reason: BlockedReason, headers: Record<string, string[] | string | undefined>) {
super();
this.reason = reason;
this.headers = headers;
}
}

View file

@ -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)}`);

View file

@ -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: {

View file

@ -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.';
}