Files
n8n-enterprise-unlocked/packages/workflow/src/ErrorReporterProxy.ts
T
Iván Ovejero 67702c2485 refactor(core): Switch plain errors in workflow to ApplicationError (no-changelog) (#7877)
Ensure all errors in `workflow` are `ApplicationError` or children of it
and contain no variables in the message, to continue normalizing all the
errors we report to Sentry

Follow-up to: https://github.com/n8n-io/n8n/pull/7873
2023-11-30 12:46:45 +01:00

38 lines
1.0 KiB
TypeScript

import * as Logger from './LoggerProxy';
import { ApplicationError, type ReportingOptions } from './errors/application.error';
interface ErrorReporter {
report: (error: Error | string, options?: ReportingOptions) => void;
}
const instance: ErrorReporter = {
report: (error) => {
if (error instanceof Error) {
let e = error;
do {
const meta = e instanceof ApplicationError ? e.extra : undefined;
Logger.error(`${e.constructor.name}: ${e.message}`, meta);
e = e.cause as Error;
} while (e);
}
},
};
export function init(errorReporter: ErrorReporter) {
instance.report = errorReporter.report;
}
const wrap = (e: unknown) => {
if (e instanceof Error) return e;
if (typeof e === 'string') return new ApplicationError(e);
return;
};
export const error = (e: unknown, options?: ReportingOptions) => {
const toReport = wrap(e);
if (toReport) instance.report(toReport, options);
};
export const warn = (warning: Error | string, options?: ReportingOptions) =>
error(warning, { level: 'warning', ...options });