Skip to content

Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.

fix: Fix ErrorHandler type (#3881)

Jan Buchar50982482e9c3
diff --git a/docs/public-api/crawlee-basic.api.md b/docs/public-api/crawlee-basic.api.md
index 16e8c55cc..5b97c00e4 100644
--- a/docs/public-api/crawlee-basic.api.md
+++ b/docs/public-api/crawlee-basic.api.md
@@ -301,8 +301,8 @@ export interface CreateContextOptions {
export { Element_2 as Element }
-// @public (undocumented)
-export type ErrorHandler<Context extends CrawlingContext = CrawlingContext, ExtendedContext extends Context = Context> = (inputs: Context & Partial<ExtendedContext>, error: Error) => Awaitable<void>;
+// @public
+export type ErrorHandler<BaseContext extends CrawlingContext = CrawlingContext, ExtendedContext extends BaseContext = BaseContext> = (inputs: BaseContext & Partial<ExtendedContext>, error: Error) => Awaitable<void>;
// @public (undocumented)
export type RequestHandler<Context extends CrawlingContext = CrawlingContext> = (inputs: Context) => Awaitable<void>;
diff --git a/docs/public-api/crawlee-cheerio.api.md b/docs/public-api/crawlee-cheerio.api.md
index 4624e9c48..8611d6759 100644
--- a/docs/public-api/crawlee-cheerio.api.md
+++ b/docs/public-api/crawlee-cheerio.api.md
@@ -10,7 +10,7 @@ import * as cheerio from 'cheerio';
import type { CheerioAPI } from 'cheerio';
import { CheerioRoot } from '@crawlee/utils';
import { ContextPipeline } from '@crawlee/http';
-import { CrawlingContext } from '@crawlee/http';
+import type { CrawlingContext } from '@crawlee/http';
import type { Dictionary } from '@crawlee/types';
import type { EnqueueLinksOptions } from '@crawlee/http';
import type { ErrorHandler } from '@crawlee/http';
@@ -59,7 +59,8 @@ JSONData extends Dictionary = any> extends InternalHttpCrawlingContext<UserData,
// @public (undocumented)
export type CheerioErrorHandler<UserData extends Dictionary = any, // with default to Dictionary we cant use a typed router in untyped crawler
-JSONData extends Dictionary = any> = ErrorHandler<CheerioCrawlingContext<UserData, JSONData>>;
+JSONData extends Dictionary = any, // with default to Dictionary we cant use a typed router in untyped crawler
+ContextExtension = Dictionary<never>> = ErrorHandler<CrawlingContext, CheerioCrawlingContext<UserData, JSONData> & ContextExtension>;
// @public (undocumented)
export type CheerioHook<UserData extends Dictionary = any, // with default to Dictionary we cant use a typed router in untyped crawler
diff --git a/docs/public-api/crawlee-http.api.md b/docs/public-api/crawlee-http.api.md
index da7d20e74..8c8775db4 100644
--- a/docs/public-api/crawlee-http.api.md
+++ b/docs/public-api/crawlee-http.api.md
@@ -86,7 +86,8 @@ export interface FileDownloadCrawlingContext<UserData extends Dictionary = any>
}
// @public (undocumented)
-export type FileDownloadErrorHandler<UserData extends Dictionary = any> = ErrorHandler<FileDownloadCrawlingContext<UserData>>;
+export type FileDownloadErrorHandler<UserData extends Dictionary = any, // with default to Dictionary we cant use a typed router in untyped crawler
+ContextExtension = Dictionary<never>> = ErrorHandler<CrawlingContext_2, FileDownloadCrawlingContext<UserData> & ContextExtension>;
// @public (undocumented)
export type FileDownloadHook<UserData extends Dictionary = any> = InternalHttpHook<FileDownloadCrawlingContext<UserData>>;
@@ -217,7 +218,8 @@ export interface HttpCrawlingContext<UserData extends Dictionary = any, JSONData
// @public (undocumented)
export type HttpErrorHandler<UserData extends Dictionary = any, // with default to Dictionary we cant use a typed router in untyped crawler
-JSONData extends JsonValue = any> = ErrorHandler<HttpCrawlingContext<UserData, JSONData>>;
+JSONData extends JsonValue = any, // with default to Dictionary we cant use a typed router in untyped crawler
+ContextExtension = Dictionary<never>> = ErrorHandler<CrawlingContext, HttpCrawlingContext<UserData, JSONData> & ContextExtension>;
// @public (undocumented)
export type HttpHook<UserData extends Dictionary = any, // with default to Dictionary we cant use a typed router in untyped crawler
diff --git a/docs/public-api/crawlee-jsdom.api.md b/docs/public-api/crawlee-jsdom.api.md
index c5befb5cc..c3584fdb8 100644
--- a/docs/public-api/crawlee-jsdom.api.md
+++ b/docs/public-api/crawlee-jsdom.api.md
@@ -12,7 +12,7 @@ import { BooleanPredicate } from 'ow';
import { CheerioAPI } from 'cheerio';
import { CheerioRoot } from '@crawlee/utils';
import { ContextPipeline } from '@crawlee/http';
-import { CrawlingContext } from '@crawlee/http';
+import type { CrawlingContext } from '@crawlee/http';
import type { Dictionary } from '@crawlee/types';
import type { DOMWindow } from 'jsdom';
import type { EnqueueLinksOptions } from '@crawlee/http';
@@ -136,7 +136,8 @@ JSONData extends Dictionary = any> extends InternalHttpCrawlingContext<UserData,
// @public (undocumented)
export type JSDOMErrorHandler<UserData extends Dictionary = any, // with default to Dictionary we cant use a typed router in untyped crawler
-JSONData extends Dictionary = any> = ErrorHandler<JSDOMCrawlingContext<UserData, JSONData>>;
+JSONData extends Dictionary = any, // with default to Dictionary we cant use a typed router in untyped crawler
+ContextExtension = Dictionary<never>> = ErrorHandler<CrawlingContext, JSDOMCrawlingContext<UserData, JSONData> & ContextExtension>;
// @public (undocumented)
export type JSDOMHook<UserData extends Dictionary = any, // with default to Dictionary we cant use a typed router in untyped crawler
diff --git a/docs/public-api/crawlee-linkedom.api.md b/docs/public-api/crawlee-linkedom.api.md
index c102e6795..77051c2b5 100644
--- a/docs/public-api/crawlee-linkedom.api.md
+++ b/docs/public-api/crawlee-linkedom.api.md
@@ -8,7 +8,7 @@ import type { BasicCrawlingContext } from '@crawlee/http';
import * as cheerio from 'cheerio';
import { CheerioRoot } from '@crawlee/utils';
import { ContextPipeline } from '@crawlee/http';
-import { CrawlingContext } from '@crawlee/http';
+import type { CrawlingContext } from '@crawlee/http';
import type { Dictionary } from '@crawlee/types';
import type { EnqueueLinksOptions } from '@crawlee/http';
import type { ErrorHandler } from '@crawlee/http';
@@ -72,7 +72,8 @@ JSONData extends Dictionary = any> extends InternalHttpCrawlingContext<UserData,
// @public (undocumented)
export type LinkeDOMErrorHandler<UserData extends Dictionary = any, // with default to Dictionary we cant use a typed router in untyped crawler
-JSONData extends Dictionary = any> = ErrorHandler<LinkeDOMCrawlingContext<UserData, JSONData>>;
+JSONData extends Dictionary = any, // with default to Dictionary we cant use a typed router in untyped crawler
+ContextExtension = Dictionary<never>> = ErrorHandler<CrawlingContext, LinkeDOMCrawlingContext<UserData, JSONData> & ContextExtension>;
// @public (undocumented)
export type LinkeDOMHook<UserData extends Dictionary = any, // with default to Dictionary we cant use a typed router in untyped crawler
diff --git a/packages/basic-crawler/src/internals/basic-crawler.ts b/packages/basic-crawler/src/internals/basic-crawler.ts
index b613dd4cb..67f47e292 100644
--- a/packages/basic-crawler/src/internals/basic-crawler.ts
+++ b/packages/basic-crawler/src/internals/basic-crawler.ts
@@ -126,10 +126,18 @@ const deferredCleanupKey = Symbol('deferredCleanup');
export type RequestHandler<Context extends CrawlingContext = CrawlingContext> = (inputs: Context) => Awaitable<void>;
+/**
+ * An error handler receives the crawling context and the error that was thrown while processing the request.
+ *
+ * Unlike the {@apilink RequestHandler}, an error handler may run before the context pipeline has finished
+ * building the full context (e.g. when navigation or session setup fails). Therefore only `BaseContext` is
+ * guaranteed to be present, while the extra properties added by the pipeline and `extendContext` (the
+ * difference between `BaseContext` and `ExtendedContext`) are only available as a `Partial`.
+ */
export type ErrorHandler<
- Context extends CrawlingContext = CrawlingContext,
- ExtendedContext extends Context = Context,
-> = (inputs: Context & Partial<ExtendedContext>, error: Error) => Awaitable<void>;
+ BaseContext extends CrawlingContext = CrawlingContext,
+ ExtendedContext extends BaseContext = BaseContext,
+> = (inputs: BaseContext & Partial<ExtendedContext>, error: Error) => Awaitable<void>;
export interface StatusMessageCallbackParams<
Context extends CrawlingContext = BasicCrawlingContext,
diff --git a/packages/cheerio-crawler/src/internals/cheerio-crawler.ts b/packages/cheerio-crawler/src/internals/cheerio-crawler.ts
index 342350afe..6954d7d1f 100644
--- a/packages/cheerio-crawler/src/internals/cheerio-crawler.ts
+++ b/packages/cheerio-crawler/src/internals/cheerio-crawler.ts
@@ -1,5 +1,6 @@
import type {
BasicCrawlingContext,
+ CrawlingContext,
EnqueueLinksOptions,
ErrorHandler,
GetUserDataFromRequest,
@@ -30,7 +31,8 @@ import { parseDocument } from 'htmlparser2';
export type CheerioErrorHandler<
UserData extends Dictionary = any, // with default to Dictionary we cant use a typed router in untyped crawler
JSONData extends Dictionary = any, // with default to Dictionary we cant use a typed router in untyped crawler
-> = ErrorHandler<CheerioCrawlingContext<UserData, JSONData>>;
+ ContextExtension = Dictionary<never>,
+> = ErrorHandler<CrawlingContext, CheerioCrawlingContext<UserData, JSONData> & ContextExtension>;
export interface CheerioCrawlerOptions<
ContextExtension = Dictionary<never>,
diff --git a/packages/http-crawler/src/internals/file-download.ts b/packages/http-crawler/src/internals/file-download.ts
index 81dd217d0..0faf96a71 100644
--- a/packages/http-crawler/src/internals/file-download.ts
+++ b/packages/http-crawler/src/internals/file-download.ts
@@ -14,7 +14,8 @@ const kBodyDrained = Symbol('bodyDrained');
export type FileDownloadErrorHandler<
UserData extends Dictionary = any, // with default to Dictionary we cant use a typed router in untyped crawler
-> = ErrorHandler<FileDownloadCrawlingContext<UserData>>;
+ ContextExtension = Dictionary<never>,
+> = ErrorHandler<CrawlingContext, FileDownloadCrawlingContext<UserData> & ContextExtension>;
export type FileDownloadHook<
UserData extends Dictionary = any, // with default to Dictionary we cant use a typed router in untyped crawler
diff --git a/packages/http-crawler/src/internals/http-crawler.ts b/packages/http-crawler/src/internals/http-crawler.ts
index 3a961e129..8d312d5a8 100644
--- a/packages/http-crawler/src/internals/http-crawler.ts
+++ b/packages/http-crawler/src/internals/http-crawler.ts
@@ -57,7 +57,8 @@ const HTTP_OPTIMIZED_AUTOSCALED_POOL_OPTIONS: AutoscaledPoolOptions = {
export type HttpErrorHandler<
UserData extends Dictionary = any, // with default to Dictionary we cant use a typed router in untyped crawler
JSONData extends JsonValue = any, // with default to Dictionary we cant use a typed router in untyped crawler
-> = ErrorHandler<HttpCrawlingContext<UserData, JSONData>>;
+ ContextExtension = Dictionary<never>,
+> = ErrorHandler<CrawlingContext, HttpCrawlingContext<UserData, JSONData> & ContextExtension>;
export interface HttpCrawlerOptions<
Context extends InternalHttpCrawlingContext = InternalHttpCrawlingContext,
diff --git a/packages/jsdom-crawler/src/internals/jsdom-crawler.ts b/packages/jsdom-crawler/src/internals/jsdom-crawler.ts
index 2735dffe1..6eaff6040 100644
--- a/packages/jsdom-crawler/src/internals/jsdom-crawler.ts
+++ b/packages/jsdom-crawler/src/internals/jsdom-crawler.ts
@@ -1,5 +1,6 @@
import type {
BasicCrawlingContext,
+ CrawlingContext,
EnqueueLinksOptions,
ErrorHandler,
GetUserDataFromRequest,
@@ -33,7 +34,8 @@ import { addTimeoutToPromise } from '@apify/timeout';
export type JSDOMErrorHandler<
UserData extends Dictionary = any, // with default to Dictionary we cant use a typed router in untyped crawler
JSONData extends Dictionary = any, // with default to Dictionary we cant use a typed router in untyped crawler
-> = ErrorHandler<JSDOMCrawlingContext<UserData, JSONData>>;
+ ContextExtension = Dictionary<never>,
+> = ErrorHandler<CrawlingContext, JSDOMCrawlingContext<UserData, JSONData> & ContextExtension>;
export interface JSDOMCrawlerOptions<
ContextExtension = Dictionary<never>,
diff --git a/packages/linkedom-crawler/src/internals/linkedom-crawler.ts b/packages/linkedom-crawler/src/internals/linkedom-crawler.ts
index 7e0b76842..4d3b60b3f 100644
--- a/packages/linkedom-crawler/src/internals/linkedom-crawler.ts
+++ b/packages/linkedom-crawler/src/internals/linkedom-crawler.ts
@@ -1,5 +1,6 @@
import type {
BasicCrawlingContext,
+ CrawlingContext,
EnqueueLinksOptions,
ErrorHandler,
GetUserDataFromRequest,
@@ -30,7 +31,8 @@ import { DOMParser } from 'linkedom/cached';
export type LinkeDOMErrorHandler<
UserData extends Dictionary = any, // with default to Dictionary we cant use a typed router in untyped crawler
JSONData extends Dictionary = any, // with default to Dictionary we cant use a typed router in untyped crawler
-> = ErrorHandler<LinkeDOMCrawlingContext<UserData, JSONData>>;
+ ContextExtension = Dictionary<never>,
+> = ErrorHandler<CrawlingContext, LinkeDOMCrawlingContext<UserData, JSONData> & ContextExtension>;
export interface LinkeDOMCrawlerOptions<
ContextExtension = Dictionary<never>,
diff --git a/test/core/crawlers/error_handler_types.test.ts b/test/core/crawlers/error_handler_types.test.ts
new file mode 100644
index 000000000..5ddc9a191
--- /dev/null
+++ b/test/core/crawlers/error_handler_types.test.ts
@@ -0,0 +1,88 @@
+import type { CrawlingContext, ErrorHandler, RequestHandler } from '@crawlee/basic';
+import type { BrowserCrawlerOptions, BrowserCrawlingContext } from '@crawlee/browser';
+import type { CheerioCrawlerOptions, CheerioErrorHandler, CheerioRequestHandler } from '@crawlee/cheerio';
+import type { HttpCrawlerOptions, HttpCrawlingContext, HttpErrorHandler } from '@crawlee/http';
+import type { PlaywrightCrawlerOptions, PlaywrightCrawlingContext } from '@crawlee/playwright';
+import type { Dictionary } from '@crawlee/utils';
+
+/**
+ * Type-level regression test for https://github.com/apify/crawlee/issues/3424.
+ */
+describe('ErrorHandler option types (#3424)', () => {
+ test('cheerio - explicitly typed handlers via CheerioErrorHandler', () => {
+ const requestHandler: CheerioRequestHandler = async ({ request }) => void request.url;
+ const failedRequestHandler: CheerioErrorHandler = async ({ request }, error) => void [request, error];
+
+ const options: CheerioCrawlerOptions = {
+ requestHandler,
+ errorHandler: failedRequestHandler,
+ failedRequestHandler,
+ };
+
+ expect(options).toBeTruthy();
+ });
+
+ test('cheerio - handler carrying an extendContext extension stays assignable', () => {
+ // The idiomatic pattern: define extendContext as a named function and derive everything from it.
+ const extendContext = async () => ({
+ customHelper: async () => {},
+ });
+ type Extension = Awaited<ReturnType<typeof extendContext>>;
+
+ // The concrete context AND the inferred extendContext additions are visible in the handler...
+ const failedRequestHandler: CheerioErrorHandler<Dictionary, Dictionary, Extension> = async (context, error) => {
+ await context.customHelper?.();
+ void [context.request, error];
+ };
+
+ // ...and it remains assignable to a crawler configured with that extendContext.
+ const options: CheerioCrawlerOptions<Extension> = {
+ extendContext,
+ errorHandler: failedRequestHandler,
+ failedRequestHandler,
+ };
+
+ expect(options).toBeTruthy();
+ });
+
+ test('http - explicitly typed handlers via HttpErrorHandler', () => {
+ const failedRequestHandler: HttpErrorHandler = async ({ request }, error) => void [request, error];
+
+ const options: HttpCrawlerOptions<HttpCrawlingContext> = {
+ errorHandler: failedRequestHandler,
+ failedRequestHandler,
+ };
+
+ expect(options).toBeTruthy();
+ });
+
+ test('browser - explicitly typed handlers via ErrorHandler<PlaywrightCrawlingContext>', () => {
+ const requestHandler: RequestHandler<PlaywrightCrawlingContext> = async ({ request }) => void request.url;
+ const failedRequestHandler: ErrorHandler<CrawlingContext, PlaywrightCrawlingContext> = async (
+ { request },
+ error,
+ ) => void [request, error];
+
+ const options: PlaywrightCrawlerOptions = {
+ requestHandler,
+ errorHandler: failedRequestHandler,
+ failedRequestHandler,
+ };
+
+ expect(options).toBeTruthy();
+ });
+
+ test('browser - explicitly typed handlers via ErrorHandler<BrowserCrawlingContext>', () => {
+ const failedRequestHandler: ErrorHandler<CrawlingContext, BrowserCrawlingContext> = async (
+ { request },
+ error,
+ ) => void [request, error];
+
+ const options: BrowserCrawlerOptions = {
+ errorHandler: failedRequestHandler,
+ failedRequestHandler,
+ };
+
+ expect(options).toBeTruthy();
+ });
+});