n8n/packages/cli/src/ErrorReporting.ts
कारतोफ्फेलस्क्रिप्ट™ 6127c958f5
fix: Do not crash the server when Telemetry is blocked via DNS (#4947)
* do not crash the process on unhandled axios errors

* postHog.capture does not return a promise
2022-12-16 13:23:24 +01:00

51 lines
1.3 KiB
TypeScript

import * as Sentry from '@sentry/node';
import { RewriteFrames } from '@sentry/integrations';
import type { Application } from 'express';
import config from '@/config';
import { ErrorReporterProxy } from 'n8n-workflow';
let initialized = false;
export const initErrorHandling = () => {
if (initialized) return;
if (!config.getEnv('diagnostics.enabled')) {
initialized = true;
return;
}
// Collect longer stacktraces
Error.stackTraceLimit = 50;
const dsn = config.getEnv('diagnostics.config.sentry.dsn');
const { N8N_VERSION: release, ENVIRONMENT: environment } = process.env;
Sentry.init({
dsn,
release,
environment,
integrations: (integrations) => {
integrations = integrations.filter(({ name }) => name !== 'OnUncaughtException');
integrations.push(new RewriteFrames({ root: process.cwd() }));
return integrations;
},
});
process.on('uncaughtException', (error) => {
ErrorReporterProxy.error(error);
if (error.constructor?.name !== 'AxiosError') throw error;
});
ErrorReporterProxy.init({
report: (error, options) => Sentry.captureException(error, options),
});
initialized = true;
};
export const setupErrorMiddleware = (app: Application) => {
const { requestHandler, errorHandler } = Sentry.Handlers;
app.use(requestHandler());
app.use(errorHandler());
};