chore: add url info to blocked error

This commit is contained in:
WebStreamr 2025-09-12 18:35:02 +00:00
parent 491595934f
commit a729dc85e0
No known key found for this signature in database
4 changed files with 15 additions and 17 deletions

View file

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

View file

@ -17,16 +17,12 @@ export * from './TooManyTimeoutsError';
export const logErrorAndReturnNiceString = (ctx: Context, logger: winston.Logger, source: string, error: unknown): string => {
if (error instanceof BlockedError) {
if (error.reason === BlockedReason.cloudflare_challenge) {
logger.warn(`${source}: Request was blocked via Cloudflare challenge.`, ctx);
} else if (error.reason === BlockedReason.cloudflare_censor) {
logger.warn(`${source}: Request was censored by Cloudflare.`, ctx);
} else if (error.reason === BlockedReason.media_flow_proxy_auth) {
if (error.reason === BlockedReason.media_flow_proxy_auth) {
return '⚠️ MediaFlow Proxy authentication failed. Please set the correct password.';
} else {
logger.warn(`${source}: Request was blocked, headers: ${JSON.stringify(error.headers)}.`, ctx);
}
logger.warn(`${source}: Request to ${error.url.href} was blocked, reason: ${error.reason}, headers: ${JSON.stringify(error.headers)}.`, ctx);
return `⚠️ Request was blocked. Reason: ${error.reason}`;
}

View file

@ -131,7 +131,7 @@ export class Fetcher {
const noFlareSolverr = init?.noFlareSolverr ?? false;
const flareSolverrEndpoint = envGet('FLARESOLVERR_ENDPOINT');
if (noFlareSolverr || !flareSolverrEndpoint) {
throw new BlockedError(BlockedReason.cloudflare_challenge, httpCacheItem.headers);
throw new BlockedError(url, BlockedReason.cloudflare_challenge, httpCacheItem.headers);
}
this.logger.info(`Query FlareSolverr for ${url.href}`, ctx);
@ -140,7 +140,7 @@ export class Fetcher {
const challengeResult = await (await this.queuedFetch(ctx, new URL(flareSolverrEndpoint), { method: 'POST', body: JSON.stringify(body), headers: { 'Content-Type': 'application/json' }, queueLimit: 1, timeout: 15000 })).json() as FlareSolverrResult;
if (challengeResult.status !== 'ok') {
this.logger.warn(`FlareSolverr issue: ${JSON.stringify(challengeResult)}`, ctx);
throw new BlockedError(BlockedReason.flaresolverr_failed, {});
throw new BlockedError(url, BlockedReason.flaresolverr_failed, {});
}
challengeResult.solution.cookies.forEach((cookie) => {
@ -168,14 +168,14 @@ export class Fetcher {
if (httpCacheItem.status === 403) {
if (ctx.config.mediaFlowProxyUrl && url.href.startsWith(ctx.config.mediaFlowProxyUrl)) {
throw new BlockedError(BlockedReason.media_flow_proxy_auth, httpCacheItem.headers);
throw new BlockedError(url, BlockedReason.media_flow_proxy_auth, httpCacheItem.headers);
}
throw new BlockedError(BlockedReason.unknown, httpCacheItem.headers);
throw new BlockedError(url, BlockedReason.unknown, httpCacheItem.headers);
}
if (httpCacheItem.status === 451) {
throw new BlockedError(BlockedReason.cloudflare_censor, httpCacheItem.headers);
throw new BlockedError(url, BlockedReason.cloudflare_censor, httpCacheItem.headers);
}
if (httpCacheItem.status === 429) {

View file

@ -110,7 +110,7 @@ describe('resolve', () => {
url: new URL('https://example.com'),
format: Format.unknown,
isExternal: true,
error: new BlockedError(BlockedReason.cloudflare_challenge, {}),
error: new BlockedError(new URL('https://example.com'), BlockedReason.cloudflare_challenge, {}),
label: 'hoster.com',
sourceId: '',
meta: {
@ -121,7 +121,7 @@ describe('resolve', () => {
url: new URL('https://example.com'),
format: Format.unknown,
isExternal: true,
error: new BlockedError(BlockedReason.cloudflare_censor, {}),
error: new BlockedError(new URL('https://example.com'), BlockedReason.cloudflare_censor, {}),
label: 'hoster.com',
sourceId: '',
meta: {
@ -132,7 +132,7 @@ describe('resolve', () => {
url: new URL('https://example.com'),
format: Format.unknown,
isExternal: true,
error: new BlockedError(BlockedReason.media_flow_proxy_auth, {}),
error: new BlockedError(new URL('https://example.com'), BlockedReason.media_flow_proxy_auth, {}),
label: 'hoster.com',
sourceId: '',
meta: {
@ -143,7 +143,7 @@ describe('resolve', () => {
url: new URL('https://example.com'),
format: Format.unknown,
isExternal: true,
error: new BlockedError(BlockedReason.unknown, {}),
error: new BlockedError(new URL('https://example.com'), BlockedReason.unknown, {}),
label: 'hoster.com',
sourceId: '',
meta: {