Files
n8n-enterprise-unlocked/packages/cli/src/ErrorReporting.ts
Iván Ovejero dff8456382 refactor(core): Reorganize error hierarchy in core and workflow packages (no-changelog) (#7820)
Ensure all errors in `core` and `workflow` inherit from
`ApplicationError` so that we start normalizing all the errors we report
to Sentry

Follow-up to:
https://github.com/n8n-io/n8n/pull/7757#discussion_r1404338844

### `core` package

`ApplicationError`
- `FileSystemError` (abstract)
	- `FileNotFoundError`
	- `DisallowedFilepathError`
- `BinaryDataError` (abstract)
	- `InvalidModeError`
	- `InvalidManagerError`
- `InvalidExecutionMetadataError`

### `workflow` package

`ApplicationError`
- `ExecutionBaseError` (abstract)
	- `WorkflowActivationError`
		- `WorkflowDeactivationError`
		- `WebhookTakenError`
	- `WorkflowOperationError`
		- `SubworkflowOperationError`
			- `CliWorkflowOperationError`
	- `ExpressionError`
		- `ExpressionExtensionError`
	- `NodeError` (abstract)
		- `NodeOperationError`
		- `NodeApiError`
	- `NodeSSLError`

Up next:
- Reorganize errors in `cli`
- Flatten the hierarchy in `workflow` (do we really need
`ExecutionBaseError`?)
- Remove `ExecutionError` type
- Stop throwing plain `Error`s
- Replace `severity` with `level`
- Add node and credential types as `tags`
- Add workflow IDs and execution IDs as `extras`
2023-11-27 15:33:21 +01:00

66 lines
1.8 KiB
TypeScript

import { createHash } from 'crypto';
import config from '@/config';
import { ErrorReporterProxy, ApplicationError, ExecutionBaseError } from 'n8n-workflow';
let initialized = false;
export const initErrorHandling = async () => {
if (initialized) return;
process.on('uncaughtException', (error) => {
ErrorReporterProxy.error(error);
});
const dsn = config.getEnv('diagnostics.config.sentry.dsn');
if (!config.getEnv('diagnostics.enabled') || !dsn) {
initialized = true;
return;
}
// Collect longer stacktraces
Error.stackTraceLimit = 50;
const { N8N_VERSION: release, ENVIRONMENT: environment } = process.env;
const { init, captureException, addGlobalEventProcessor } = await import('@sentry/node');
const { RewriteFrames } = await import('@sentry/integrations');
init({
dsn,
release,
environment,
integrations: (integrations) => {
integrations = integrations.filter(({ name }) => name !== 'OnUncaughtException');
integrations.push(new RewriteFrames({ root: process.cwd() }));
return integrations;
},
});
const seenErrors = new Set<string>();
addGlobalEventProcessor((event, { originalException }) => {
if (originalException instanceof ExecutionBaseError && originalException.severity === 'warning')
return null;
if (originalException instanceof ApplicationError) {
const { level, extra } = originalException;
if (level === 'warning') return null;
event.level = level;
if (extra) event.extra = { ...event.extra, ...extra };
}
if (!event.exception) return null;
const eventHash = createHash('sha1').update(JSON.stringify(event.exception)).digest('base64');
if (seenErrors.has(eventHash)) return null;
seenErrors.add(eventHash);
return event;
});
ErrorReporterProxy.init({
report: (error, options) => captureException(error, options),
});
initialized = true;
};