From a4c0cc9b5c56639067918f1bad1baf4eb201e48b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=A4=95=E0=A4=BE=E0=A4=B0=E0=A4=A4=E0=A5=8B=E0=A4=AB?= =?UTF-8?q?=E0=A5=8D=E0=A4=AB=E0=A5=87=E0=A4=B2=E0=A4=B8=E0=A5=8D=E0=A4=95?= =?UTF-8?q?=E0=A5=8D=E0=A4=B0=E0=A4=BF=E0=A4=AA=E0=A5=8D=E0=A4=9F=E2=84=A2?= Date: Mon, 15 May 2023 13:54:48 +0000 Subject: [PATCH] feat(core): Reduce the number of events sent to Sentry (#6235) --- packages/cli/src/ErrorReporting.ts | 16 ++++++++++-- .../src/UserManagement/PermissionChecker.ts | 1 + packages/core/src/NodeExecuteFunctions.ts | 14 +++++++++-- packages/workflow/src/NodeErrors.ts | 25 +++++++++++++++---- packages/workflow/src/RoutingNode.ts | 2 ++ 5 files changed, 49 insertions(+), 9 deletions(-) diff --git a/packages/cli/src/ErrorReporting.ts b/packages/cli/src/ErrorReporting.ts index 4b71cb9b6b..ab40038080 100644 --- a/packages/cli/src/ErrorReporting.ts +++ b/packages/cli/src/ErrorReporting.ts @@ -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; @@ -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'); @@ -32,6 +33,17 @@ export const initErrorHandling = async () => { }, }); + const seenErrors = new Set(); + 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; diff --git a/packages/cli/src/UserManagement/PermissionChecker.ts b/packages/cli/src/UserManagement/PermissionChecker.ts index fb2e7b19e1..d2327159d4 100644 --- a/packages/cli/src/UserManagement/PermissionChecker.ts +++ b/packages/cli/src/UserManagement/PermissionChecker.ts @@ -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', }); } diff --git a/packages/core/src/NodeExecuteFunctions.ts b/packages/core/src/NodeExecuteFunctions.ts index a7efac1158..b32a67f32d 100644 --- a/packages/core/src/NodeExecuteFunctions.ts +++ b/packages/core/src/NodeExecuteFunctions.ts @@ -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' }, ); } @@ -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' }, ); } @@ -1745,6 +1747,7 @@ export async function getCredentials( throw new NodeOperationError( node, `Node type "${node.type}" does not have any credentials defined!`, + { severity: 'warning' }, ); } @@ -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' }, ); } @@ -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 diff --git a/packages/workflow/src/NodeErrors.ts b/packages/workflow/src/NodeErrors.ts index 4c8e014e42..86edf912e5 100644 --- a/packages/workflow/src/NodeErrors.ts +++ b/packages/workflow/src/NodeErrors.ts @@ -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. */ @@ -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 }); @@ -234,6 +238,7 @@ interface NodeOperationErrorOptions { description?: string; runIndex?: number; itemIndex?: number; + severity?: Severity; } /** @@ -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; @@ -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); diff --git a/packages/workflow/src/RoutingNode.ts b/packages/workflow/src/RoutingNode.ts index 727fe5efca..dde7d31d43 100644 --- a/packages/workflow/src/RoutingNode.ts +++ b/packages/workflow/src/RoutingNode.ts @@ -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), }); } }