Skip to content

Commit

Permalink
fix(core): trigger errorHandler for session errors (#2683)
Browse files Browse the repository at this point in the history
Closes #2678
  • Loading branch information
B4nan authored Sep 27, 2024
1 parent f3927e6 commit 7d72bcb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 19 deletions.
21 changes: 10 additions & 11 deletions packages/basic-crawler/src/internals/basic-crawler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import type {
StatisticState,
StatisticsOptions,
LoadedContext,
RestrictedCrawlingContext,
} from '@crawlee/core';
import {
AutoscaledPool,
Expand Down Expand Up @@ -99,14 +100,13 @@ export interface BasicCrawlingContext<UserData extends Dictionary = Dictionary>
*/
const SAFE_MIGRATION_WAIT_MILLIS = 20000;

export type RequestHandler<Context extends CrawlingContext = BasicCrawlingContext> = (
inputs: LoadedContext<Context>,
) => Awaitable<void>;
export type RequestHandler<
Context extends CrawlingContext = LoadedContext<BasicCrawlingContext & RestrictedCrawlingContext>,
> = (inputs: LoadedContext<Context>) => Awaitable<void>;

export type ErrorHandler<Context extends CrawlingContext = BasicCrawlingContext> = (
inputs: LoadedContext<Context>,
error: Error,
) => Awaitable<void>;
export type ErrorHandler<
Context extends CrawlingContext = LoadedContext<BasicCrawlingContext & RestrictedCrawlingContext>,
> = (inputs: LoadedContext<Context>, error: Error) => Awaitable<void>;

export interface StatusMessageCallbackParams<
Context extends CrawlingContext = BasicCrawlingContext,
Expand Down Expand Up @@ -1444,13 +1444,12 @@ export class BasicCrawler<Context extends CrawlingContext = BasicCrawlingContext

if (shouldRetryRequest) {
await this.stats.errorTrackerRetry.addAsync(error, crawlingContext);
await this._tagUserHandlerError(() =>
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) {
Expand Down
8 changes: 0 additions & 8 deletions test/core/crawlers/basic_crawler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 7d72bcb

Please sign in to comment.