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.

feat: error tracker (#1467)

Szymon Marczak6bfe1ce0161f
diff --git a/packages/basic-crawler/src/internals/basic-crawler.ts b/packages/basic-crawler/src/internals/basic-crawler.ts
index c522b0523..430daf2cb 100644
--- a/packages/basic-crawler/src/internals/basic-crawler.ts
+++ b/packages/basic-crawler/src/internals/basic-crawler.ts
@@ -594,6 +594,14 @@ export class BasicCrawler<Context extends CrawlingContext = BasicCrawlingContext
};
this.log.info('Crawl finished. Final request statistics:', stats);
+ if (this.stats.errorTracker.total !== 0) {
+ this.log.info(`Error analysis:`, {
+ totalErrors: this.stats.errorTracker.total,
+ uniqueErrors: this.stats.errorTracker.getUniqueErrorCount(),
+ mostCommonErrors: Object.fromEntries(this.stats.errorTracker.getMostPopularErrors(3)),
+ });
+ }
+
const client = this.config.getStorageClient();
if (client.teardown) {
@@ -974,6 +982,8 @@ export class BasicCrawler<Context extends CrawlingContext = BasicCrawlingContext
if (shouldRetryRequest) {
request.retryCount++;
+ this.stats.errorTrackerRetry.add(error);
+
await this._tagUserHandlerError(() => this.errorHandler?.(this._augmentContextWithDeprecatedError(crawlingContext, error), error));
const { url, retryCount, id } = request;
@@ -988,6 +998,8 @@ export class BasicCrawler<Context extends CrawlingContext = BasicCrawlingContext
await source.reclaimRequest(request);
} else {
+ this.stats.errorTracker.add(error);
+
// If we get here, the request is either not retryable
// or failed more than retryCount times and will not be retried anymore.
// Mark the request as failed and do not retry.
diff --git a/packages/browser-crawler/src/internals/browser-crawler.ts b/packages/browser-crawler/src/internals/browser-crawler.ts
index 7e6cc2fbb..c45b7e80c 100644
--- a/packages/browser-crawler/src/internals/browser-crawler.ts
+++ b/packages/browser-crawler/src/internals/browser-crawler.ts
@@ -582,6 +582,12 @@ export abstract class BrowserCrawler<
protected async _responseHandler(crawlingContext: Context): Promise<void> {
const { response, session, request, page } = crawlingContext;
+ if (typeof response === 'object' && typeof response.status === 'function') {
+ const status: number = response.status();
+
+ this.stats.registerStatusCode(status);
+ }
+
if (this.sessionPool && response && session) {
if (typeof response === 'object' && typeof response.status === 'function') {
this._throwOnBlockedRequest(session, response.status());
diff --git a/packages/core/src/crawlers/statistics.ts b/packages/core/src/crawlers/statistics.ts
index ed2a81139..5be9ec09e 100644
--- a/packages/core/src/crawlers/statistics.ts
+++ b/packages/core/src/crawlers/statistics.ts
@@ -1,4 +1,5 @@
import ow from 'ow';
+import { ErrorTracker } from '@crawlee/utils';
import { log as defaultLog } from '../log';
import { KeyValueStore } from '../storages/key_value_store';
import type { EventManager } from '../events/event_manager';
@@ -28,6 +29,15 @@ class Job {
}
}
+const errorTrackerConfig = {
+ showErrorCode: true,
+ showErrorName: true,
+ showStackTrace: true,
+ showFullStack: false,
+ showErrorMessage: true,
+ showFullMessage: false,
+};
+
/**
* The statistics class provides an interface to collecting and logging run
* statistics for requests.
@@ -41,6 +51,16 @@ class Job {
export class Statistics {
private static id = 0;
+ /**
+ * An error tracker for final retry errors.
+ */
+ errorTracker = new ErrorTracker(errorTrackerConfig);
+
+ /**
+ * An error tracker for retry errors prior to the final retry.
+ */
+ errorTrackerRetry = new ErrorTracker(errorTrackerConfig);
+
/**
* Statistic instance id.
*/
@@ -99,6 +119,9 @@ export class Statistics {
* Set the current statistic instance to pristine values
*/
reset() {
+ this.errorTracker.reset();
+ this.errorTrackerRetry.reset();
+
this.state = {
requestsFinished: 0,
requestsFailed: 0,
@@ -113,6 +136,9 @@ export class Statistics {
crawlerFinishedAt: null,
statsPersistedAt: null,
crawlerRuntimeMillis: 0,
+ requestsWithStatusCode: {},
+ errors: this.errorTracker.result,
+ retryErrors: this.errorTrackerRetry.result,
};
this.requestRetryHistogram.length = 0;
@@ -122,6 +148,19 @@ export class Statistics {
this._teardown();
}
+ /**
+ * Increments the status code counter.
+ */
+ registerStatusCode(code: number) {
+ const s = String(code);
+
+ if (this.state.requestsWithStatusCode[s] === undefined) {
+ this.state.requestsWithStatusCode[s] = 0;
+ }
+
+ this.state.requestsWithStatusCode[s]++;
+ }
+
/**
* Starts a job
* @ignore
@@ -303,7 +342,7 @@ export class Statistics {
// merge all the current state information that can be used from the outside
// without the need to reconstruct for the sake of stats.calculate()
// omit duplicated information
- return {
+ const result = {
...this.state,
crawlerLastStartTimestamp: this.instanceStart,
crawlerFinishedAt: this.state.crawlerFinishedAt ? new Date(this.state.crawlerFinishedAt).toISOString() : null,
@@ -313,6 +352,16 @@ export class Statistics {
statsPersistedAt: new Date().toISOString(),
...this.calculate(),
};
+
+ Reflect.deleteProperty(result, 'requestsWithStatusCode');
+ Reflect.deleteProperty(result, 'errors');
+ Reflect.deleteProperty(result, 'retryErrors');
+
+ result.requestsWithStatusCode = this.state.requestsWithStatusCode;
+ result.errors = this.state.errors;
+ result.retryErrors = this.state.retryErrors;
+
+ return result;
}
}
@@ -357,4 +406,7 @@ export interface StatisticState {
crawlerFinishedAt: Date | string | null;
crawlerRuntimeMillis: number;
statsPersistedAt: Date | string | null;
+ errors: Record<string, unknown>;
+ retryErrors: Record<string, unknown>;
+ requestsWithStatusCode: Record<string, number>;
}
diff --git a/packages/http-crawler/src/internals/http-crawler.ts b/packages/http-crawler/src/internals/http-crawler.ts
index b85ca5e19..a386d9afb 100644
--- a/packages/http-crawler/src/internals/http-crawler.ts
+++ b/packages/http-crawler/src/internals/http-crawler.ts
@@ -571,6 +571,10 @@ export class HttpCrawler<Context extends InternalHttpCrawlingContext<any, any, H
const { response, encoding } = this._encodeResponse(request, responseStream, charset);
const contentType = { type, encoding };
+ if (statusCode! >= 400 && statusCode! <= 599) {
+ this.stats.registerStatusCode(statusCode!);
+ }
+
if (statusCode! >= 500) {
const body = await readStreamToString(response, encoding);
diff --git a/packages/utils/src/index.ts b/packages/utils/src/index.ts
index 2d39306bc..01970c28b 100644
--- a/packages/utils/src/index.ts
+++ b/packages/utils/src/index.ts
@@ -6,4 +6,5 @@ export * from './internals/memory-info';
export * from './internals/debug';
export * as social from './internals/social';
export * from './internals/typedefs';
+export * from './internals/error_tracker';
export { Dictionary, Awaitable, Constructor } from '@crawlee/types';
diff --git a/packages/utils/src/internals/error_tracker.ts b/packages/utils/src/internals/error_tracker.ts
new file mode 100644
index 000000000..a06862a87
--- /dev/null
+++ b/packages/utils/src/internals/error_tracker.ts
@@ -0,0 +1,366 @@
+/**
+ * Node.js Error interface
+ */
+ interface ErrnoException extends Error {
+ errno?: number | undefined;
+ code?: string | number | undefined;
+ path?: string | undefined;
+ syscall?: string | undefined;
+ cause?: any;
+}
+
+export interface ErrorTrackerOptions {
+ showErrorCode: boolean;
+ showErrorName: boolean;
+ showStackTrace: boolean;
+ showFullStack: boolean;
+ showErrorMessage: boolean;
+ showFullMessage: boolean;
+}
+
+const extractPathFromStackTraceLine = (line: string) => {
+ const lastStartingRoundBracketIndex = line.lastIndexOf('(');
+
+ if (lastStartingRoundBracketIndex !== -1) {
+ const closingRoundBracketIndex = line.indexOf(')', lastStartingRoundBracketIndex);
+
+ if (closingRoundBracketIndex !== -1) {
+ return line.slice(lastStartingRoundBracketIndex + 1, closingRoundBracketIndex);
+ }
+ }
+
+ return line;
+};
+
+// https://v8.dev/docs/stack-trace-api#appendix%3A-stack-trace-format
+const getPathFromStackTrace = (stack: string[]) => {
+ for (const line of stack) {
+ const path = extractPathFromStackTraceLine(line);
+
+ if (
+ path.startsWith('node:')
+ || path.includes('/node_modules/')
+ || path.includes('\\node_modules\\')
+ ) {
+ continue;
+ }
+
+ return path;
+ }
+
+ return extractPathFromStackTraceLine(stack[0]);
+};
+
+const getStackTraceGroup = (error: ErrnoException, storage: Record<string, unknown>, showFullStack: boolean) => {
+ const stack = error.stack?.split('\n').map((line) => line.trim());
+
+ let sliceAt = -1;
+
+ if (stack) {
+ for (let i = 0; i < stack.length; i++) {
+ if (stack[i].startsWith('at ') || stack[i].startsWith('eval at ')) {
+ sliceAt = i;
+ break;
+ }
+ }
+ }
+
+ let normalizedStackTrace = null;
+ if (sliceAt !== -1) {
+ normalizedStackTrace = showFullStack ? stack!.slice(sliceAt).map((x) => x.trim()).join('\n') : getPathFromStackTrace(stack!.slice(sliceAt));
+ }
+
+ if (!normalizedStackTrace) {
+ normalizedStackTrace = 'missing stack trace';
+ }
+
+ if (!(normalizedStackTrace in storage)) {
+ storage[normalizedStackTrace] = Object.create(null);
+ }
+
+ return storage[normalizedStackTrace] as Record<string, unknown>;
+};
+
+const getErrorCodeGroup = (error: ErrnoException, storage: Record<string, unknown>) => {
+ let { code } = error;
+
+ if (code === undefined) {
+ code = 'missing error code';
+ }
+
+ if (!(code in storage)) {
+ storage[code] = Object.create(null);
+ }
+
+ return storage[String(code)] as Record<string, unknown>;
+};
+
+const getErrorNameGroup = (error: ErrnoException, storage: Record<string, unknown>) => {
+ const { name } = error;
+
+ if (!(name in storage)) {
+ storage[name] = Object.create(null);
+ }
+
+ return storage[name] as Record<string, unknown>;
+};
+
+const findBiggestWordIntersection = (a: string[], b: string[]) => {
+ let maxStreak = 0;
+ let bStreakIndex = -1;
+ let aStreakIndex = -1;
+ for (let aIndex = 0; aIndex < a.length; aIndex++) {
+ let bIndex = -1;
+
+ do {
+ let aWalkIndex = aIndex;
+
+ bIndex = b.indexOf(a[aIndex], bIndex + 1);
+
+ let bWalkIndex = bIndex;
+
+ let streak = 0;
+ while (aWalkIndex < a.length && bWalkIndex < b.length && b[bWalkIndex++] === a[aWalkIndex++]) {
+ streak++;
+ }
+
+ if (streak > maxStreak) {
+ maxStreak = streak;
+ aStreakIndex = aIndex;
+ bStreakIndex = bIndex;
+ }
+ } while (bIndex !== -1);
+ }
+
+ return {
+ maxStreak,
+ aStreakIndex,
+ bStreakIndex,
+ };
+};
+
+const arrayCount = (array: unknown[], target: unknown) => {
+ let result = 0;
+
+ for (const item of array) {
+ if (item === target) {
+ result++;
+ }
+ }
+
+ return result;
+};
+
+const calculatePlaceholder = (a: string[], b: string[]) => {
+ const { maxStreak, aStreakIndex, bStreakIndex } = findBiggestWordIntersection(a, b);
+
+ if (maxStreak === 0) {
+ return ['_'];
+ }
+
+ const leftA = a.slice(0, aStreakIndex);
+ const leftB = b.slice(0, bStreakIndex);
+ const rightA = a.slice(aStreakIndex + maxStreak);
+ const rightB = b.slice(bStreakIndex + maxStreak);
+
+ const output: string[] = [];
+
+ if (leftA.length !== 0 || leftB.length !== 0) {
+ output.push(...calculatePlaceholder(leftA, leftB));
+ }
+
+ output.push(...a.slice(aStreakIndex, aStreakIndex + maxStreak));
+
+ if (rightA.length !== 0 || rightB.length !== 0) {
+ output.push(...calculatePlaceholder(rightA, rightB));
+ }
+
+ return output;
+};
+
+const normalizedCalculatePlaceholder = (a: string[], b: string[]) => {
+ const output = calculatePlaceholder(a, b);
+
+ // We can't be too general
+ if ((arrayCount(output, '_') / output.length) >= 0.5) {
+ return ['_'];
+ }
+
+ return output;
+};
+
+// Merge A (missing placeholders) into B (can contain placeholders but does not have to)
+const mergeMessages = (a: string, b: string, storage: Record<string, unknown>) => {
+ const placeholder = normalizedCalculatePlaceholder(
+ a.split(' '),
+ b.split(' '),
+ ).join(' ');
+
+ if (placeholder === '_') {
+ return undefined;
+ }
+
+ interface HasCount {
+ count: number;
+ }
+
+ const count = (storage[a] as HasCount).count + (storage[b] as HasCount).count;
+
+ delete storage[a];
+ delete storage[b];
+
+ storage[placeholder] = Object.assign(Object.create(null), {
+ count,
+ });
+
+ return placeholder;
+};
+
+const getErrorMessageGroup = (error: ErrnoException, storage: Record<string, unknown>, showFullMessage: boolean) => {
+ let { message } = error;
+
+ if (!showFullMessage) {
+ const newLineIndex = message.indexOf('\n');
+ message = message.slice(0, newLineIndex === -1 ? undefined : newLineIndex);
+ }
+
+ if (!(message in storage)) {
+ storage[message] = Object.assign(Object.create(null), {
+ count: 0,
+ });
+
+ // This actually safe, since we Object.create(null) so no prototype pollution can happen.
+ // eslint-disable-next-line no-restricted-syntax, guard-for-in
+ for (const existingMessage in storage) {
+ const newMessage = mergeMessages(message, existingMessage, storage);
+ if (newMessage) {
+ message = newMessage;
+ break;
+ }
+ }
+ }
+
+ return storage[message] as Record<string, unknown>;
+};
+
+const increaseCount = (group: { count: number }) => {
+ if (!('count' in group)) {
+ // In case users don't want to display error message
+ group.count = 0;
+ }
+
+ group.count++;
+};
+
+/**
+ * This class tracks errors and computes a summary of information like:
+ * - where the errors happened
+ * - what the error names are
+ * - what the error codes are
+ * - what is the general error message
+ *
+ * This is extremely useful when there are dynamic error messages, such as argument validation.
+ *
+ * Since the structure of the `tracker.result` object differs when using different options,
+ * it's typed as `Record<string, unknown>`. The most deep object has a `count` property, which is a number.
+ *
+ * It's possible to get the total amount of errors via the `tracker.total` property.
+ */
+export class ErrorTracker {
+ #options: ErrorTrackerOptions;
+
+ result: Record<string, unknown>;
+
+ total: number;
+
+ constructor(options: Partial<ErrorTrackerOptions> = {}) {
+ this.#options = {
+ showErrorCode: true,
+ showErrorName: true,
+ showStackTrace: true,
+ showFullStack: false,
+ showErrorMessage: true,
+ showFullMessage: false,
+ ...options,
+ };
+
+ this.result = Object.create(null);
+ this.total = 0;
+ }
+
+ add(error: ErrnoException) {
+ this.total++;
+
+ let group = this.result;
+
+ if (this.#options.showStackTrace) {
+ group = getStackTraceGroup(error, group, this.#options.showFullStack);
+ }
+
+ if (this.#options.showErrorCode) {
+ group = getErrorCodeGroup(error, group);
+ }
+
+ if (this.#options.showErrorName) {
+ group = getErrorNameGroup(error, group);
+ }
+
+ if (this.#options.showErrorMessage) {
+ group = getErrorMessageGroup(error, group, this.#options.showFullMessage);
+ }
+
+ increaseCount(group as { count: number });
+
+ if (typeof error.cause === 'object' && error.cause !== null) {
+ this.add(error.cause);
+ }
+ }
+
+ getUniqueErrorCount() {
+ let count = 0;
+
+ const goDeeper = (group: Record<string, unknown>): void => {
+ if ('count' in group) {
+ count++;
+ return;
+ }
+
+ // eslint-disable-next-line guard-for-in, no-restricted-syntax
+ for (const key in group) {
+ goDeeper(group[key] as Record<string, unknown>);
+ }
+ };
+
+ goDeeper(this.result);
+
+ return count;
+ }
+
+ getMostPopularErrors(count: number) {
+ const result: [number, string[]][] = [];
+
+ const goDeeper = (group: Record<string, unknown>, path: string[]): void => {
+ if ('count' in group) {
+ result.push([(group as any).count, path]);
+ return;
+ }
+
+ // eslint-disable-next-line guard-for-in, no-restricted-syntax
+ for (const key in group) {
+ goDeeper(group[key] as Record<string, unknown>, [...path, key]);
+ }
+ };
+
+ goDeeper(this.result, []);
+
+ return result.sort((a, b) => a[0] - b[0]).slice(0, count);
+ }
+
+ reset() {
+ // This actually safe, since we Object.create(null) so no prototype pollution can happen.
+ // eslint-disable-next-line no-restricted-syntax, guard-for-in
+ for (const key in this.result) {
+ delete this.result[key];
+ }
+ }
+};
diff --git a/test/core/error_tracker.test.ts b/test/core/error_tracker.test.ts
new file mode 100644
index 000000000..58435f17d
--- /dev/null
+++ b/test/core/error_tracker.test.ts
@@ -0,0 +1,743 @@
+/* eslint-disable no-multi-spaces */
+import exp from 'node:constants';
+import { ErrorTracker } from '../../packages/utils/src/internals/error_tracker';
+
+const random = () => Math.random().toString(36).slice(2);
+
+const g = (error: { name?: string; message?: string; code?: string | number; stack?: string; cause?: any }) => {
+ return {
+ name: random(),
+ message: random(),
+ code: 'code' in error ? error.code : random(),
+ stack: random(),
+ ...error,
+ };
+};
+
+const s = (stack: string) => {
+ const evalIndex = stack.indexOf('eval at ');
+ const atIndex = stack.indexOf('at ');
+ const index = evalIndex === -1 ? atIndex : evalIndex;
+
+ return stack.slice(index).split('\n').map((line) => line.trim()).join('\n');
+};
+
+// A case for
+// https://github.com/microsoft/playwright/blob/99d1ad5a88c3e89360829eee92dbaa98d75beaa4/packages/playwright-core/src/server/dispatchers/dispatcher.ts#L329
+const multilineError = {
+ name: 'TypeError',
+ message: 'Invalid URL',
+ code: 'ERR_INVALID_URL',
+ stack: [
+ 'TypeError: Invalid URL',
+ 'foobar',
+ ' eval at Foo.a (eval at Bar.z (node:url))',
+ ' eval at Foo.a (eval at Bar.z (myscript.js:10:3))',
+ ' eval at Foo.a (eval at Bar.z (node:http))',
+ ].join('\n'),
+ cause: undefined as any,
+} as const;
+
+const error = {
+ name: 'TypeError',
+ message: 'Invalid URL',
+ code: 'ERR_INVALID_URL',
+ stack: 'eval at Foo.a (eval at Bar.z (myscript.js:10:3))',
+ cause: undefined as any,
+} as const;
+
+const errorNoCode = {
+ name: 'TypeError',
+ message: 'Invalid URL',
+ stack: 'eval at Foo.a (eval at Bar.z (myscript.js:10:3))',
+ code: undefined as undefined,
+ cause: undefined as any,
+} as const;
+
+const errorRandomCode = {
+ name: 'TypeError',
+ message: 'Invalid URL',
+ stack: 'eval at Foo.a (eval at Bar.z (myscript.js:10:3))',
+ cause: undefined as any,
+} as const;
+
+const errorRandomName = {
+ message: 'Invalid URL',
+ code: 'ERR_INVALID_URL',
+ stack: 'eval at Foo.a (eval at Bar.z (myscript.js:10:3))',
+ cause: undefined as any,
+} as const;
+
+const errorRandomMessage = {
+ name: 'TypeError',
+ code: 'ERR_INVALID_URL',
+ stack: 'eval at Foo.a (eval at Bar.z (myscript.js:10:3))',
+ cause: undefined as any,
+} as const;
+
+const errorRandomStack = {
+ name: 'TypeError',
+ message: 'Invalid URL',
+ code: 'ERR_INVALID_URL',
+ cause: undefined as any,
+} as const;
+
+test('works', () => {
+ const tracker = new ErrorTracker({
+ showErrorCode: true,
+ showErrorMessage: true,
+ showErrorName: true,
+ showStackTrace: true,
+ showFullStack: false,
+ showFullMessage: true,
+ });
+
+ const e = error;
+
+ expect(tracker.result).toMatchObject({});
+
+ tracker.add(g(e));
+
+ expect(tracker.result).toMatchObject({
+ 'myscript.js:10:3': { // source
+ [e.code]: { // code
+ [e.name]: { // name
+ [e.message]: { // message
+ count: 1,
+ },
+ },
+ },
+ },
+ });
+ expect(tracker.getUniqueErrorCount()).toBe(1);
+
+ tracker.add(g(e));
+
+ expect(tracker.result).toMatchObject({
+ 'myscript.js:10:3': { // source
+ [e.code]: { // code
+ [e.name]: { // name
+ [e.message]: { // message
+ count: 2,
+ },
+ },
+ },
+ },
+ });
+ expect(tracker.getUniqueErrorCount()).toBe(1);
+});
+
+test('no code is null code', () => {
+ const tracker = new ErrorTracker({
+ showErrorCode: true,
+ showErrorMessage: true,
+ showErrorName: true,
+ showStackTrace: true,
+ showFullStack: false,
+ showFullMessage: true,
+ });
+
+ const e = errorNoCode;
+
+ tracker.add(g(e));
+ tracker.add(g(e));
+
+ expect(tracker.result).toMatchObject({
+ 'myscript.js:10:3': { // source
+ 'missing error code': { // code
+ [e.name]: { // name
+ [e.message]: { // message
+ count: 2,
+ },
+ },
+ },
+ },
+ });
+ expect(tracker.getUniqueErrorCount()).toBe(1);
+});
+
+test('can hide error code', () => {
+ const tracker = new ErrorTracker({
+ showErrorCode: false,
+ showErrorMessage: true,
+ showErrorName: true,
+ showStackTrace: true,
+ showFullStack: false,
+ showFullMessage: true,
+ });
+
+ const e = errorRandomCode;
+
+ tracker.add(g(errorRandomCode));
+ tracker.add(g(errorRandomCode));
+
+ expect(tracker.result).toMatchObject({
+ 'myscript.js:10:3': { // source
+ [e.name]: { // name
+ [e.message]: { // message
+ count: 2,
+ },
+ },
+ },
+ });
+ expect(tracker.getUniqueErrorCount()).toBe(1);
+});
+
+test('can hide error name', () => {
+ const tracker = new ErrorTracker({
+ showErrorCode: true,
+ showErrorMessage: true,
+ showErrorName: false,
+ showStackTrace: true,
+ showFullStack: false,
+ showFullMessage: true,
+ });
+
+ const e = errorRandomName;
+
+ tracker.add(g(e));
+ tracker.add(g(e));
+
+ expect(tracker.result).toMatchObject({
+ 'myscript.js:10:3': { // source
+ [e.code]: { // code
+ [e.message]: { // message
+ count: 2,
+ },
+ },
+ },
+ });
+ expect(tracker.getUniqueErrorCount()).toBe(1);
+});
+
+test('can hide error message', () => {
+ const tracker = new ErrorTracker({
+ showErrorCode: true,
+ showErrorMessage: false,
+ showErrorName: true,
+ showStackTrace: true,
+ showFullStack: false,
+ showFullMessage: true,
+ });
+
+ const e = errorRandomMessage;
+
+ tracker.add(g(errorRandomMessage));
+ tracker.add(g(errorRandomMessage));
+
+ expect(tracker.result).toMatchObject({
+ 'myscript.js:10:3': { // source
+ [e.code]: { // code
+ [e.name]: { // name
+ count: 2,
+ },
+ },
+ },
+ });
+ expect(tracker.getUniqueErrorCount()).toBe(1);
+});
+
+test('can hide error stack', () => {
+ const tracker = new ErrorTracker({
+ showErrorCode: true,
+ showErrorMessage: true,
+ showErrorName: true,
+ showStackTrace: false,
+ showFullStack: false,
+ showFullMessage: true,
+ });
+
+ tracker.add(g(errorRandomStack));
+ tracker.add(g(errorRandomStack));
+
+ expect(tracker.result).toMatchObject({
+ 'ERR_INVALID_URL': { // code
+ 'TypeError': { // name
+ 'Invalid URL': { // message
+ count: 2,
+ },
+ },
+ },
+ });
+ expect(tracker.getUniqueErrorCount()).toBe(1);
+});
+
+test('can display full stack', () => {
+ const tracker = new ErrorTracker({
+ showErrorCode: true,
+ showErrorMessage: true,
+ showErrorName: true,
+ showStackTrace: true,
+ showFullStack: true,
+ showFullMessage: true,
+ });
+
+ const e = multilineError;
+
+ tracker.add(g(e));
+ tracker.add(g(e));
+
+ expect(tracker.result).toMatchObject({
+ [s(e.stack)]: { // source
+ [e.code]: { // code
+ [e.name]: { // name
+ [e.message]: { // message
+ count: 2,
+ },
+ },
+ },
+ },
+ });
+ expect(tracker.getUniqueErrorCount()).toBe(1);
+});
+
+test('stack looks for user files first', () => {
+ const tracker = new ErrorTracker({
+ showErrorCode: true,
+ showErrorMessage: true,
+ showErrorName: true,
+ showStackTrace: true,
+ showFullStack: false,
+ showFullMessage: true,
+ });
+
+ const e = multilineError;
+
+ tracker.add(g(e));
+ tracker.add(g(e));
+
+ expect(tracker.result).toMatchObject({
+ 'myscript.js:10:3': { // source
+ [e.code]: { // code
+ [e.name]: { // name
+ [e.message]: { // message
+ count: 2,
+ },
+ },
+ },
+ },
+ });
+ expect(tracker.getUniqueErrorCount()).toBe(1);
+});
+
+test('can shorten the message to the first line', () => {
+ const tracker = new ErrorTracker({
+ showErrorCode: true,
+ showErrorMessage: true,
+ showErrorName: true,
+ showStackTrace: true,
+ showFullStack: false,
+ showFullMessage: false,
+ });
+
+ const e = g(multilineError);
+
+ tracker.add(e);
+
+ expect(tracker.result).toMatchObject({
+ 'myscript.js:10:3': { // source
+ [e.code]: { // code
+ [e.name]: { // name
+ [e.message.split('\n')[0]]: { // message
+ count: 1,
+ },
+ },
+ },
+ },
+ });
+ expect(tracker.getUniqueErrorCount()).toBe(1);
+});
+
+test('supports error.cause', () => {
+ const tracker = new ErrorTracker({
+ showErrorCode: true,
+ showErrorMessage: true,
+ showErrorName: true,
+ showStackTrace: true,
+ showFullStack: false,
+ showFullMessage: false,
+ });
+
+ const e = g(multilineError);
+ e.cause = g(errorRandomMessage);
+
+ tracker.add(e);
+
+ expect(tracker.result).toMatchObject({
+ 'myscript.js:10:3': { // source
+ [e.code]: { // code
+ [e.name]: { // name
+ [e.message.split('\n')[0]]: { // message
+ count: 1,
+ },
+ [e.cause.message]: {
+ count: 1,
+ },
+ },
+ },
+ },
+ });
+ expect(tracker.getUniqueErrorCount()).toBe(2);
+});
+
+test('placeholder #1', () => {
+ const tracker = new ErrorTracker({
+ showErrorCode: true,
+ showErrorMessage: true,
+ showErrorName: true,
+ showStackTrace: true,
+ showFullStack: false,
+ showFullMessage: true,
+ });
+
+ tracker.add({
+ name: 'Error',
+ message: 'Expected boolean, got number',
+ });
+
+ tracker.add({
+ name: 'Error',
+ message: 'Expected boolean, got string',
+ });
+
+ tracker.add({
+ name: 'Error',
+ message: 'Expected boolean, got undefined',
+ });
+
+ expect(tracker.result).toMatchObject({
+ 'missing stack trace': { // source
+ 'missing error code': { // code
+ Error: { // name
+ 'Expected boolean, got _': { // message
+ count: 3,
+ },
+ },
+ },
+ },
+ });
+ expect(tracker.getUniqueErrorCount()).toBe(1);
+});
+
+test('placeholder #2', () => {
+ const tracker = new ErrorTracker({
+ showErrorCode: true,
+ showErrorMessage: true,
+ showErrorName: true,
+ showStackTrace: true,
+ showFullStack: false,
+ showFullMessage: true,
+ });
+
+ tracker.add({
+ name: 'Error',
+ message: 'Expected `boolean`, got `number`',
+ });
+
+ tracker.add({
+ name: 'Error',
+ message: 'Expected `boolean`, got `string`',
+ });
+
+ tracker.add({
+ name: 'Error',
+ message: 'Expected `boolean`, got `undefined`',
+ });
+
+ expect(tracker.result).toMatchObject({
+ 'missing stack trace': { // source
+ 'missing error code': { // code
+ Error: { // name
+ 'Expected `boolean`, got _': { // message
+ count: 3,
+ },
+ },
+ },
+ },
+ });
+ expect(tracker.getUniqueErrorCount()).toBe(1);
+});
+
+test('placeholder #3', () => {
+ const tracker = new ErrorTracker({
+ showErrorCode: true,
+ showErrorMessage: true,
+ showErrorName: true,
+ showStackTrace: true,
+ showFullStack: false,
+ showFullMessage: true,
+ });
+
+ tracker.add({
+ name: 'Error',
+ message: '1 2 3',
+ });
+
+ tracker.add({
+ name: 'Error',
+ message: '1 4 3',
+ });
+
+ tracker.add({
+ name: 'Error',
+ message: '1 5 3',
+ });
+
+ expect(tracker.result).toMatchObject({
+ 'missing stack trace': { // source
+ 'missing error code': { // code
+ Error: { // name
+ '1 _ 3': { // message
+ count: 3,
+ },
+ },
+ },
+ },
+ });
+ expect(tracker.getUniqueErrorCount()).toBe(1);
+});
+
+test('placeholder #4', () => {
+ const tracker = new ErrorTracker({
+ showErrorCode: true,
+ showErrorMessage: true,
+ showErrorName: true,
+ showStackTrace: true,
+ showFullStack: false,
+ showFullMessage: true,
+ });
+
+ tracker.add({
+ name: 'Error',
+ message: '1 2 3',
+ });
+
+ tracker.add({
+ name: 'Error',
+ message: '1 2 4',
+ });
+
+ tracker.add({
+ name: 'Error',
+ message: '1 2 5',
+ });
+
+ expect(tracker.result).toMatchObject({
+ 'missing stack trace': { // source
+ 'missing error code': { // code
+ Error: { // name
+ '1 2 _': { // message
+ count: 3,
+ },
+ },
+ },
+ },
+ });
+ expect(tracker.getUniqueErrorCount()).toBe(1);
+});
+
+test('placeholder #5', () => {
+ const tracker = new ErrorTracker({
+ showErrorCode: true,
+ showErrorMessage: true,
+ showErrorName: true,
+ showStackTrace: true,
+ showFullStack: false,
+ showFullMessage: true,
+ });
+
+ tracker.add({
+ name: 'Error',
+ message: '1 2 3',
+ });
+
+ tracker.add({
+ name: 'Error',
+ message: '4 2 3',
+ });
+
+ tracker.add({
+ name: 'Error',
+ message: '5 2 3',
+ });
+
+ expect(tracker.result).toMatchObject({
+ 'missing stack trace': { // source
+ 'missing error code': { // code
+ Error: { // name
+ '_ 2 3': { // message
+ count: 3,
+ },
+ },
+ },
+ },
+ });
+ expect(tracker.getUniqueErrorCount()).toBe(1);
+});
+
+test('placeholder #6', () => {
+ const tracker = new ErrorTracker({
+ showErrorCode: true,
+ showErrorMessage: true,
+ showErrorName: true,
+ showStackTrace: true,
+ showFullStack: false,
+ showFullMessage: true,
+ });
+
+ tracker.add({
+ name: 'Error',
+ message: 'The weather is sunny today, but the grass is wet.',
+ });
+
+ tracker.add({
+ name: 'Error',
+ message: 'The weather is rainy today, but the grass is still dry.',
+ });
+
+ tracker.add({
+ name: 'Error',
+ message: 'The weather is wild today, however the grass is yellow.',
+ });
+
+ expect(tracker.result).toMatchObject({
+ 'missing stack trace': { // source
+ 'missing error code': { // code
+ Error: { // name
+ 'The weather is _ today, _ the grass is _': { // message
+ count: 3,
+ },
+ },
+ },
+ },
+ });
+ expect(tracker.getUniqueErrorCount()).toBe(1);
+});
+
+test('placeholder #7', () => {
+ const tracker = new ErrorTracker({
+ showErrorCode: true,
+ showErrorMessage: true,
+ showErrorName: true,
+ showStackTrace: true,
+ showFullStack: false,
+ showFullMessage: true,
+ });
+
+ tracker.add({
+ name: 'Error',
+ message: 'Expected `boolean`, got `number`',
+ });
+
+ tracker.add({
+ name: 'Error',
+ message: 'Expected `boolean`, got `number`',
+ });
+
+ expect(tracker.result).toMatchObject({
+ 'missing stack trace': { // source
+ 'missing error code': { // code
+ Error: { // name
+ 'Expected `boolean`, got `number`': { // message
+ count: 2,
+ },
+ },
+ },
+ },
+ });
+ expect(tracker.getUniqueErrorCount()).toBe(1);
+
+ tracker.add({
+ name: 'Error',
+ message: 'Expected `boolean`, got `falsy value`',
+ });
+
+ expect(tracker.result).toMatchObject({
+ 'missing stack trace': { // source
+ 'missing error code': { // code
+ Error: { // name
+ 'Expected `boolean`, got _': { // message
+ count: 3,
+ },
+ },
+ },
+ },
+ });
+ expect(tracker.getUniqueErrorCount()).toBe(1);
+});
+
+test('placeholder #8', () => {
+ const tracker = new ErrorTracker({
+ showErrorCode: true,
+ showErrorMessage: true,
+ showErrorName: true,
+ showStackTrace: true,
+ showFullStack: false,
+ showFullMessage: true,
+ });
+
+ tracker.add({
+ name: 'Error',
+ message: 'Expected `boolean`, got `number`',
+ });
+
+ tracker.add({
+ name: 'Error',
+ message: 'Expected `string`, got `null`',
+ });
+
+ expect(tracker.result).toMatchObject({
+ 'missing stack trace': { // source
+ 'missing error code': { // code
+ Error: { // name
+ 'Expected `boolean`, got `number`': { // message
+ count: 1,
+ },
+ 'Expected `string`, got `null`': {
+ count: 1,
+ },
+ },
+ },
+ },
+ });
+ expect(tracker.getUniqueErrorCount()).toBe(2);
+});
+
+test('placeholder #9', () => {
+ const tracker = new ErrorTracker({
+ showErrorCode: true,
+ showErrorMessage: true,
+ showErrorName: true,
+ showStackTrace: true,
+ showFullStack: false,
+ showFullMessage: true,
+ });
+
+ tracker.add({
+ name: 'Error',
+ message: 'Unexpected `show` property in `options` object',
+ });
+
+ tracker.add({
+ name: 'Error',
+ message: 'Missing `display` in style',
+ });
+
+ const expected = {
+ 'missing stack trace': { // source
+ 'missing error code': { // code
+ Error: { // name
+ 'Unexpected `show` property in `options` object': { // message
+ count: 1,
+ },
+ 'Missing `display` in style': {
+ count: 1,
+ },
+ },
+ },
+ },
+ };
+
+ expect(tracker.result).toMatchObject(expected);
+ expect(tracker.getUniqueErrorCount()).toBe(2);
+});