From 7d72bcb36f32933c6251382e5efd28a284e9267d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Ad=C3=A1mek?= Date: Fri, 27 Sep 2024 11:00:12 +0200 Subject: [PATCH] fix(core): trigger `errorHandler` for session errors (#2683) Closes #2678 --- .../src/internals/basic-crawler.ts | 21 +++++++++---------- test/core/crawlers/basic_crawler.test.ts | 8 ------- 2 files changed, 10 insertions(+), 19 deletions(-) diff --git a/packages/basic-crawler/src/internals/basic-crawler.ts b/packages/basic-crawler/src/internals/basic-crawler.ts index f10a498dd626..19635417fabb 100644 --- a/packages/basic-crawler/src/internals/basic-crawler.ts +++ b/packages/basic-crawler/src/internals/basic-crawler.ts @@ -26,6 +26,7 @@ import type { StatisticState, StatisticsOptions, LoadedContext, + RestrictedCrawlingContext, } from '@crawlee/core'; import { AutoscaledPool, @@ -99,14 +100,13 @@ export interface BasicCrawlingContext */ const SAFE_MIGRATION_WAIT_MILLIS = 20000; -export type RequestHandler = ( - inputs: LoadedContext, -) => Awaitable; +export type RequestHandler< + Context extends CrawlingContext = LoadedContext, +> = (inputs: LoadedContext) => Awaitable; -export type ErrorHandler = ( - inputs: LoadedContext, - error: Error, -) => Awaitable; +export type ErrorHandler< + Context extends CrawlingContext = LoadedContext, +> = (inputs: LoadedContext, error: Error) => Awaitable; export interface StatusMessageCallbackParams< Context extends CrawlingContext = BasicCrawlingContext, @@ -1444,13 +1444,12 @@ export class BasicCrawler + this.errorHandler?.(this._augmentContextWithDeprecatedError(crawlingContext, error), error), + ); if (error instanceof SessionError) { await this._rotateSession(crawlingContext); - } else { - await this._tagUserHandlerError(() => - this.errorHandler?.(this._augmentContextWithDeprecatedError(crawlingContext, error), error), - ); } if (!request.noRetry) { diff --git a/test/core/crawlers/basic_crawler.test.ts b/test/core/crawlers/basic_crawler.test.ts index 53af604e8d54..3a9879cd479a 100644 --- a/test/core/crawlers/basic_crawler.test.ts +++ b/test/core/crawlers/basic_crawler.test.ts @@ -1019,9 +1019,7 @@ describe('BasicCrawler', () => { requestHandler: async () => sleep(1e3), }); - // @ts-expect-error Overriding protected method const warningSpy = vitest.spyOn(crawler.log, 'warning'); - // @ts-expect-error Overriding protected method const errorSpy = vitest.spyOn(crawler.log, 'error'); await crawler.run(); @@ -1059,9 +1057,7 @@ describe('BasicCrawler', () => { }, }); - // @ts-expect-error Overriding protected method const warningSpy = vitest.spyOn(crawler.log, 'warning'); - // @ts-expect-error Overriding protected method const errorSpy = vitest.spyOn(crawler.log, 'error'); await crawler.run(); @@ -1100,9 +1096,7 @@ describe('BasicCrawler', () => { requestHandler: async () => sleep(1e3), }); - // @ts-expect-error Overriding protected method const warningSpy = vitest.spyOn(crawler.log, 'warning'); - // @ts-expect-error Overriding protected method const errorSpy = vitest.spyOn(crawler.log, 'error'); await crawler.run(); @@ -1145,9 +1139,7 @@ describe('BasicCrawler', () => { }, }); - // @ts-expect-error Overriding protected method const warningSpy = vitest.spyOn(crawler.log, 'warning'); - // @ts-expect-error Overriding protected method const errorSpy = vitest.spyOn(crawler.log, 'error'); await crawler.run();