From 2d7cd8310dc87c6a5596d6490e40d3cee5b67ac7 Mon Sep 17 00:00:00 2001 From: Arthur Geron <3487334+arthurgeron@users.noreply.github.com> Date: Mon, 27 Jan 2025 14:49:25 -0300 Subject: [PATCH] feat: ignore or directly send errors to sentry --- .../Error/services/ReportErrorService.tsx | 36 +++++---- .../systems/Error/utils/getErrorIgnoreData.ts | 77 +++++++++++++++++++ 2 files changed, 98 insertions(+), 15 deletions(-) create mode 100644 packages/app/src/systems/Error/utils/getErrorIgnoreData.ts diff --git a/packages/app/src/systems/Error/services/ReportErrorService.tsx b/packages/app/src/systems/Error/services/ReportErrorService.tsx index 0ca659ccc4..b842540d9b 100644 --- a/packages/app/src/systems/Error/services/ReportErrorService.tsx +++ b/packages/app/src/systems/Error/services/ReportErrorService.tsx @@ -1,7 +1,7 @@ import type { StoredFuelWalletError } from '@fuel-wallet/types'; -import * as Sentry from '@sentry/react'; import { db } from '~/systems/Core/utils/database'; import { captureException } from '~/systems/Error/utils/captureException'; +import { getErrorIgnoreData } from '~/systems/Error/utils/getErrorIgnoreData'; import { parseFuelError } from '../utils'; export class ReportErrorService { @@ -18,21 +18,27 @@ export class ReportErrorService { } static async saveError(error: Error) { - const parsedError = parseFuelError(error); - if (!parsedError) { - console.warn(`Can't save error without a message`); - return; - } - if (!('id' in parsedError)) { - console.warn(`Can't save error without an id`); - return; - } - if (!db.isOpen() || db.hasBeenClosed()) { - console.warn('Error saving error: db is closed'); - return; - } - try { + const parsedError = parseFuelError(error); + const ignoreData = getErrorIgnoreData(parsedError?.error); + if (!parsedError) { + console.warn(`Can't save error without a message`); + return; + } + if (!('id' in parsedError)) { + console.warn(`Can't save error without an id`); + return; + } + if (ignoreData?.action === 'ignore') return; + if (ignoreData?.action === 'hide') { + // Directly report to Sentry and exit + captureException(parsedError.error, parsedError.extra); + return; + } + if (!db.isOpen() || db.hasBeenClosed()) { + console.warn('Error saving error: db is closed'); + return; + } return await db.errors.add(parsedError); } catch (e) { console.warn('Failed to save error', e); diff --git a/packages/app/src/systems/Error/utils/getErrorIgnoreData.ts b/packages/app/src/systems/Error/utils/getErrorIgnoreData.ts new file mode 100644 index 0000000000..8494bb490a --- /dev/null +++ b/packages/app/src/systems/Error/utils/getErrorIgnoreData.ts @@ -0,0 +1,77 @@ +type IgnoredError = { + value: string; + field: 'message' | 'stack' | 'name'; + comparison: 'exact' | 'partial' | 'startsWith'; + /** + * @description Whether to ignore the error or hide it from Report Error screen. Avoid ignoring errors that might contain sensitive information. + */ + action: 'ignore' | 'hide'; +}; + +export function getErrorIgnoreData( + error: Error | undefined +): IgnoredError | undefined { + return IGNORED_ERRORS.find((filter) => { + const errorValue = error?.[filter.field] as string | undefined; + + switch (filter.comparison) { + case 'exact': + return filter.value === errorValue; + case 'startsWith': + return errorValue?.startsWith(filter.value); + case 'partial': + return errorValue?.includes(filter.value); + } + }); +} + +const IGNORED_ERRORS: IgnoredError[] = [ + { + value: 'Out of sync', + field: 'message', + comparison: 'exact', + action: 'hide', + }, + { + value: 'Failed to fetch', + field: 'message', + comparison: 'exact', + action: 'hide', + }, + { + value: 'TypeError:', + field: 'stack', + comparison: 'startsWith', + action: 'ignore', + }, + { + value: 'NotFoundError', + field: 'name', + comparison: 'exact', + action: 'hide', + }, + { + value: 'The browser is shutting down.', + field: 'message', + comparison: 'partial', + action: 'hide', + }, + { + value: 'Error fetching asset from db', + field: 'message', + comparison: 'partial', + action: 'hide', + }, + { + value: 'Cannot read properties of undefined', + field: 'message', + comparison: 'partial', + action: 'hide', + }, + { + value: 'Params are required', + field: 'message', + comparison: 'partial', + action: 'hide', + }, +];