Skip to content

Commit

Permalink
feat(core): Reduce the number of events sent to Sentry (#6235)
Browse files Browse the repository at this point in the history
  • Loading branch information
netroy committed May 15, 2023
1 parent 9182d15 commit a4c0cc9
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 9 deletions.
16 changes: 14 additions & 2 deletions packages/cli/src/ErrorReporting.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createHash } from 'crypto';
import config from '@/config';
import { ErrorReporterProxy } from 'n8n-workflow';
import { ErrorReporterProxy, NodeError } from 'n8n-workflow';

let initialized = false;

Expand All @@ -17,7 +18,7 @@ export const initErrorHandling = async () => {
const dsn = config.getEnv('diagnostics.config.sentry.dsn');
const { N8N_VERSION: release, ENVIRONMENT: environment } = process.env;

const { init, captureException } = await import('@sentry/node');
const { init, captureException, addGlobalEventProcessor } = await import('@sentry/node');
// eslint-disable-next-line @typescript-eslint/naming-convention
const { RewriteFrames } = await import('@sentry/integrations');

Expand All @@ -32,6 +33,17 @@ export const initErrorHandling = async () => {
},
});

const seenErrors = new Set<string>();
addGlobalEventProcessor((event, { originalException }) => {
if (originalException instanceof NodeError && originalException.severity === 'warning')
return null;
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;
});

process.on('uncaughtException', (error) => {
ErrorReporterProxy.error(error);
if (error.constructor?.name !== 'AxiosError') throw error;
Expand Down
1 change: 1 addition & 0 deletions packages/cli/src/UserManagement/PermissionChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export class PermissionChecker {

throw new NodeOperationError(nodeToFlag, 'Node has no access to credential', {
description: 'Please recreate the credential or ask its owner to share it with you.',
severity: 'warning',
});
}

Expand Down
14 changes: 12 additions & 2 deletions packages/core/src/NodeExecuteFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1392,6 +1392,7 @@ export async function httpRequestWithAuthentication(
throw new NodeOperationError(
node,
`Node "${node.name}" does not have any credentials of type "${credentialsType}" set!`,
{ severity: 'warning' },
);
}

Expand Down Expand Up @@ -1587,6 +1588,7 @@ export async function requestWithAuthentication(
throw new NodeOperationError(
node,
`Node "${node.name}" does not have any credentials of type "${credentialsType}" set!`,
{ severity: 'warning' },
);
}

Expand Down Expand Up @@ -1745,6 +1747,7 @@ export async function getCredentials(
throw new NodeOperationError(
node,
`Node type "${node.type}" does not have any credentials defined!`,
{ severity: 'warning' },
);
}

Expand All @@ -1755,6 +1758,7 @@ export async function getCredentials(
throw new NodeOperationError(
node,
`Node type "${node.type}" does not have any credentials of type "${type}" defined!`,
{ severity: 'warning' },
);
}

Expand All @@ -1778,10 +1782,16 @@ export async function getCredentials(
if (nodeCredentialDescription?.required === true) {
// Credentials are required so error
if (!node.credentials) {
throw new NodeOperationError(node, 'Node does not have any credentials set!');
throw new NodeOperationError(node, 'Node does not have any credentials set!', {
severity: 'warning',
});
}
if (!node.credentials[type]) {
throw new NodeOperationError(node, `Node does not have any credentials set for "${type}"!`);
throw new NodeOperationError(
node,
`Node does not have any credentials set for "${type}"!`,
{ severity: 'warning' },
);
}
} else {
// Credentials are not required
Expand Down
25 changes: 20 additions & 5 deletions packages/workflow/src/NodeErrors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import { parseString } from 'xml2js';
import type { IDataObject, INode, IStatusCodeMessages, JsonObject } from './Interfaces';

type Severity = 'warning' | 'error';

/**
* Top-level properties where an error message can be found in an API response.
*/
Expand Down Expand Up @@ -103,9 +105,11 @@ export abstract class ExecutionBaseError extends Error {
* Base class for specific NodeError-types, with functionality for finding
* a value recursively inside an error object.
*/
abstract class NodeError extends ExecutionBaseError {
export abstract class NodeError extends ExecutionBaseError {
node: INode;

severity: Severity = 'error';

constructor(node: INode, error: Error | JsonObject) {
const message = error instanceof Error ? error.message : '';
super(message, { cause: error });
Expand Down Expand Up @@ -234,6 +238,7 @@ interface NodeOperationErrorOptions {
description?: string;
runIndex?: number;
itemIndex?: number;
severity?: Severity;
}

/**
Expand All @@ -248,9 +253,8 @@ export class NodeOperationError extends NodeError {
}
super(node, error);

if (options.message) {
this.message = options.message;
}
if (options.message) this.message = options.message;
if (options.severity) this.severity = options.severity;
this.description = options.description;
this.context.runIndex = options.runIndex;
this.context.itemIndex = options.itemIndex;
Expand Down Expand Up @@ -296,10 +300,21 @@ export class NodeApiError extends NodeError {
constructor(
node: INode,
error: JsonObject,
{ message, description, httpCode, parseXml, runIndex, itemIndex }: NodeApiErrorOptions = {},
{
message,
description,
httpCode,
parseXml,
runIndex,
itemIndex,
severity,
}: NodeApiErrorOptions = {},
) {
super(node, error);

if (severity) this.severity = severity;
else if (httpCode?.charAt(0) !== '5') this.severity = 'warning';

if (error.error) {
// only for request library error
this.removeCircularRefs(error.error as JsonObject);
Expand Down
2 changes: 2 additions & 0 deletions packages/workflow/src/RoutingNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,13 @@ export class RoutingNode {
returnData.push({ json: {}, error: error.message });
continue;
}
if (error instanceof NodeApiError) error = error.cause;
throw new NodeApiError(this.node, error, {
runIndex,
itemIndex: i,
message: error?.message,
description: error?.description,
httpCode: error.isAxiosError && error.response && String(error.response?.status),
});
}
}
Expand Down

0 comments on commit a4c0cc9

Please sign in to comment.