refactor(core): Unify error reporters in core and task runners (#12168)

Co-authored-by: Tomi Turtiainen <10324676+tomi@users.noreply.github.com>
This commit is contained in:
Iván Ovejero
2024-12-13 17:34:37 +01:00
committed by GitHub
parent d937e5abe8
commit 120499291d
7 changed files with 33 additions and 175 deletions

View File

@@ -1,26 +1,21 @@
import { GlobalConfig } from '@n8n/config';
import type { NodeOptions } from '@sentry/node';
import { close } from '@sentry/node';
import type { ErrorEvent, EventHint } from '@sentry/types';
import { AxiosError } from 'axios';
import { ApplicationError, LoggerProxy, type ReportingOptions } from 'n8n-workflow';
import { createHash } from 'node:crypto';
import { Service } from 'typedi';
import { InstanceSettings } from './InstanceSettings';
import type { InstanceType } from './InstanceSettings';
@Service()
export class ErrorReporter {
private initialized = false;
/** Hashes of error stack traces, to deduplicate error reports. */
private seenErrors = new Set<string>();
private report: (error: Error | string, options?: ReportingOptions) => void;
constructor(
private readonly globalConfig: GlobalConfig,
private readonly instanceSettings: InstanceSettings,
) {
constructor() {
// eslint-disable-next-line @typescript-eslint/unbound-method
this.report = this.defaultReport;
}
@@ -41,18 +36,16 @@ export class ErrorReporter {
}
}
async init() {
if (this.initialized) return;
async shutdown(timeoutInMs = 1000) {
await close(timeoutInMs);
}
async init(instanceType: InstanceType | 'task_runner', dsn: string) {
process.on('uncaughtException', (error) => {
this.error(error);
});
const dsn = this.globalConfig.sentry.backendDsn;
if (!dsn) {
this.initialized = true;
return;
}
if (!dsn) return;
// Collect longer stacktraces
Error.stackTraceLimit = 50;
@@ -98,11 +91,9 @@ export class ErrorReporter {
],
});
setTag('server_type', this.instanceSettings.instanceType);
setTag('server_type', instanceType);
this.report = (error, options) => captureException(error, options);
this.initialized = true;
}
async beforeSend(event: ErrorEvent, { originalException }: EventHint) {