mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-18 10:31:15 +00:00
refactor(core): Extend error hierarchy (#12267)
This commit is contained in:
58
packages/workflow/src/errors/base/base.error.ts
Normal file
58
packages/workflow/src/errors/base/base.error.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
import type { Event } from '@sentry/node';
|
||||
import callsites from 'callsites';
|
||||
|
||||
import type { ErrorTags, ErrorLevel, ReportingOptions } from '../error.types';
|
||||
|
||||
export type BaseErrorOptions = { description?: undefined | null } & ErrorOptions & ReportingOptions;
|
||||
|
||||
/**
|
||||
* Base class for all errors
|
||||
*/
|
||||
export abstract class BaseError extends Error {
|
||||
/**
|
||||
* Error level. Defines which level the error should be logged/reported
|
||||
* @default 'error'
|
||||
*/
|
||||
readonly level: ErrorLevel;
|
||||
|
||||
/**
|
||||
* Whether the error should be reported to Sentry.
|
||||
* @default true
|
||||
*/
|
||||
readonly shouldReport: boolean;
|
||||
|
||||
readonly description: string | null | undefined;
|
||||
|
||||
readonly tags: ErrorTags;
|
||||
|
||||
readonly extra?: Event['extra'];
|
||||
|
||||
readonly packageName?: string;
|
||||
|
||||
constructor(
|
||||
message: string,
|
||||
{
|
||||
level = 'error',
|
||||
description,
|
||||
shouldReport,
|
||||
tags = {},
|
||||
extra,
|
||||
...rest
|
||||
}: BaseErrorOptions = {},
|
||||
) {
|
||||
super(message, rest);
|
||||
|
||||
this.level = level;
|
||||
this.shouldReport = shouldReport ?? (level === 'error' || level === 'fatal');
|
||||
this.description = description;
|
||||
this.tags = tags;
|
||||
this.extra = extra;
|
||||
|
||||
try {
|
||||
const filePath = callsites()[2].getFileName() ?? '';
|
||||
const match = /packages\/([^\/]+)\//.exec(filePath)?.[1];
|
||||
|
||||
if (match) this.tags.packageName = match;
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user