2023-05-15 06:54:48 -07:00
|
|
|
import { createHash } from 'crypto';
|
2022-11-09 06:25:00 -08:00
|
|
|
import config from '@/config';
|
2023-12-07 07:57:02 -08:00
|
|
|
import { ErrorReporterProxy, ApplicationError } from 'n8n-workflow';
|
2024-07-25 05:31:44 -07:00
|
|
|
// eslint-disable-next-line n8n-local-rules/misplaced-n8n-typeorm-import
|
|
|
|
import { QueryFailedError } from '@n8n/typeorm';
|
2022-11-04 09:34:47 -07:00
|
|
|
|
|
|
|
let initialized = false;
|
|
|
|
|
2023-01-02 03:14:39 -08:00
|
|
|
export const initErrorHandling = async () => {
|
2022-11-04 09:34:47 -07:00
|
|
|
if (initialized) return;
|
|
|
|
|
2023-08-14 08:26:40 -07:00
|
|
|
process.on('uncaughtException', (error) => {
|
|
|
|
ErrorReporterProxy.error(error);
|
|
|
|
});
|
|
|
|
|
2023-08-09 04:29:57 -07:00
|
|
|
const dsn = config.getEnv('diagnostics.config.sentry.dsn');
|
2023-12-18 05:26:07 -08:00
|
|
|
if (!dsn) {
|
2022-11-04 09:34:47 -07:00
|
|
|
initialized = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-21 08:54:29 -08:00
|
|
|
// Collect longer stacktraces
|
|
|
|
Error.stackTraceLimit = 50;
|
|
|
|
|
2023-12-18 05:26:07 -08:00
|
|
|
const {
|
|
|
|
N8N_VERSION: release,
|
|
|
|
ENVIRONMENT: environment,
|
|
|
|
DEPLOYMENT_NAME: serverName,
|
|
|
|
} = process.env;
|
2022-11-04 09:34:47 -07:00
|
|
|
|
2023-12-18 05:26:07 -08:00
|
|
|
const { init, captureException, addEventProcessor } = await import('@sentry/node');
|
2023-10-27 05:15:02 -07:00
|
|
|
|
2023-01-02 03:14:39 -08:00
|
|
|
const { RewriteFrames } = await import('@sentry/integrations');
|
2023-12-18 05:26:07 -08:00
|
|
|
const { Integrations } = await import('@sentry/node');
|
2023-01-02 03:14:39 -08:00
|
|
|
|
2023-12-18 05:26:07 -08:00
|
|
|
const enabledIntegrations = [
|
|
|
|
'InboundFilters',
|
|
|
|
'FunctionToString',
|
|
|
|
'LinkedErrors',
|
|
|
|
'OnUnhandledRejection',
|
|
|
|
'ContextLines',
|
|
|
|
];
|
2023-01-02 03:14:39 -08:00
|
|
|
init({
|
2022-11-04 09:34:47 -07:00
|
|
|
dsn,
|
|
|
|
release,
|
|
|
|
environment,
|
2023-12-18 05:26:07 -08:00
|
|
|
enableTracing: false,
|
|
|
|
serverName,
|
|
|
|
beforeBreadcrumb: () => null,
|
|
|
|
integrations: (integrations) => [
|
|
|
|
...integrations.filter(({ name }) => enabledIntegrations.includes(name)),
|
|
|
|
new RewriteFrames({ root: process.cwd() }),
|
|
|
|
new Integrations.RequestData({
|
|
|
|
include: {
|
|
|
|
cookies: false,
|
|
|
|
data: false,
|
|
|
|
headers: false,
|
|
|
|
query_string: false,
|
|
|
|
url: true,
|
|
|
|
user: false,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
],
|
2022-11-04 09:34:47 -07:00
|
|
|
});
|
|
|
|
|
2023-05-15 06:54:48 -07:00
|
|
|
const seenErrors = new Set<string>();
|
2023-12-18 05:26:07 -08:00
|
|
|
addEventProcessor((event, { originalException }) => {
|
|
|
|
if (!originalException) return null;
|
|
|
|
|
2024-07-25 05:31:44 -07:00
|
|
|
if (
|
|
|
|
originalException instanceof QueryFailedError &&
|
2024-08-08 03:04:34 -07:00
|
|
|
['SQLITE_FULL', 'SQLITE_IOERR'].some((errMsg) => originalException.message.includes(errMsg))
|
2024-07-25 05:31:44 -07:00
|
|
|
) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-11-27 06:33:21 -08:00
|
|
|
if (originalException instanceof ApplicationError) {
|
2023-12-08 02:30:06 -08:00
|
|
|
const { level, extra, tags } = originalException;
|
2023-11-24 05:42:46 -08:00
|
|
|
if (level === 'warning') return null;
|
|
|
|
event.level = level;
|
|
|
|
if (extra) event.extra = { ...event.extra, ...extra };
|
2023-12-08 02:30:06 -08:00
|
|
|
if (tags) event.tags = { ...event.tags, ...tags };
|
2023-11-24 05:42:46 -08:00
|
|
|
}
|
|
|
|
|
2023-12-18 05:26:07 -08:00
|
|
|
const eventHash = createHash('sha1').update(JSON.stringify(originalException)).digest('base64');
|
2023-05-15 06:54:48 -07:00
|
|
|
if (seenErrors.has(eventHash)) return null;
|
|
|
|
seenErrors.add(eventHash);
|
2023-11-24 05:42:46 -08:00
|
|
|
|
2023-05-15 06:54:48 -07:00
|
|
|
return event;
|
|
|
|
});
|
|
|
|
|
2022-11-04 09:34:47 -07:00
|
|
|
ErrorReporterProxy.init({
|
2023-01-02 03:14:39 -08:00
|
|
|
report: (error, options) => captureException(error, options),
|
2022-11-04 09:34:47 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
initialized = true;
|
|
|
|
};
|