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.
TypeScript apifyautomationcrawlercrawlingheadlessheadless-chromejavascriptnodejsnpmplaywrightpuppeteerscraperscrapingtypescriptweb-crawlerweb-crawlingweb-scraping
refactor: move `describe` helper to `@crawlee/utils` as `inspectValue` (#3586)
Richard Solar2fad6be61740
diff --git a/packages/core/src/errors.ts b/packages/core/src/errors.tsindex afc28f485..eb9f69f47 100644--- a/packages/core/src/errors.ts+++ b/packages/core/src/errors.ts@@ -1,3 +1,5 @@+import { inspectValue } from '@crawlee/utils';+/*** Errors of `NonRetryableError` type will never be retried by the crawler.*/@@ -100,16 +102,9 @@ export class RequestHandlerError extends Error {*/export class ServiceConflictError extends Error {constructor(serviceName: string, newValue: unknown, existingValue: unknown) {- const describe = (value: unknown): string => {- if (typeof value === 'object' && value !== null) {- return value.constructor.name;- }- return String(value);- };-super(`Service ${serviceName} is already in use. ` +- `Existing value: ${describe(existingValue)}, attempted new value: ${describe(newValue)}.`,+ `Existing value: ${inspectValue(existingValue)}, attempted new value: ${inspectValue(newValue)}.`,);}}diff --git a/packages/utils/src/internals/debug.ts b/packages/utils/src/internals/debug.tsindex cb068f4d1..dbb8efb49 100644--- a/packages/utils/src/internals/debug.ts+++ b/packages/utils/src/internals/debug.ts@@ -1,4 +1,5 @@import type { IncomingMessage } from 'node:http';+import { inspect } from 'node:util';import type { AllowedHttpMethods, Dictionary } from '@crawlee/types';import ow from 'ow';@@ -56,6 +57,29 @@ export function createRequestDebugInfo(};}+/**+ * Returns a human-readable label for an unknown value,+ * suitable for embedding in error messages and log output.+ *+ * Returns `constructor.name` when available (e.g. `"Configuration"`, `"Number"`),+ * otherwise falls back to `util.inspect` (e.g. for `null`, `undefined`).+ *+ * @internal+ */+export function inspectValue(value: unknown): string {+ if (typeof value === 'object' && value !== null && value.constructor?.name) {+ return value.constructor.name;+ }++ return inspect(value, {+ depth: 0,+ compact: true,+ maxStringLength: 64,+ breakLength: Infinity,+ colors: false,+ });+}+export function getObjectType(value: unknown): string {const simple = typeof value;diff --git a/test/vitest.setup.ts b/test/vitest.setup.tsindex 502c8d1ba..a73759301 100644--- a/test/vitest.setup.ts+++ b/test/vitest.setup.ts@@ -1,7 +1,6 @@import { beforeEach } from 'vitest';-import { serviceLocator } from '../packages/core/src/service_locator.js';--beforeEach(() => {+beforeEach(async () => {+ const { serviceLocator } = await import('../packages/core/src/service_locator.js');serviceLocator.reset();});