From 900174a5fdbfd165f13fa445f35ebcda10a4a128 Mon Sep 17 00:00:00 2001 From: Kiko Beats Date: Thu, 12 Sep 2024 09:37:08 +0200 Subject: [PATCH] perf(errors): avoid parse error twice --- packages/errors/index.js | 20 ++++++++++++++++++-- packages/errors/test/index.js | 7 +++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/packages/errors/index.js b/packages/errors/index.js index 4c68c46afc..c42c6d1d65 100644 --- a/packages/errors/index.js +++ b/packages/errors/index.js @@ -4,9 +4,22 @@ const debug = require('debug-logfmt')('browserless:error') const { serializeError } = require('serialize-error') const whoops = require('whoops') -const ERROR_NAME = 'BrowserlessError' +const createErrorFactory = opts => whoops('BrowserlessError', opts) -const createBrowserlessError = opts => whoops(ERROR_NAME, opts) +const markAsProcessed = error => { + Object.defineProperty(error, '__parsed', { + value: true, + writable: false, + enumerable: false, + configurable: false + }) + return error +} + +const createBrowserlessError = opts => { + const createError = createErrorFactory(opts) + return (...args) => markAsProcessed(createError(...args)) +} const browserlessError = {} @@ -28,9 +41,12 @@ browserlessError.contextDisconnected = createBrowserlessError({ }) browserlessError.ensureError = rawError => { + if (rawError.__parsed) return rawError + debug('ensureError', serializeError(rawError)) const error = 'error' in rawError ? rawError.error : rawError + const { message: errorMessage = '' } = error if ( diff --git a/packages/errors/test/index.js b/packages/errors/test/index.js index e2245de34e..8b8c436cfc 100644 --- a/packages/errors/test/index.js +++ b/packages/errors/test/index.js @@ -3,6 +3,13 @@ const test = require('ava') const errors = require('..') +test('avoid parse ensureError twice', t => { + const error = errors.ensureError({ + message: 'Protocol error (Runtime.callFunctionOn): Target closed.' + }) + t.true(error.__parsed) +}) + test('protocolError', t => { const parsedError = errors.ensureError({ message: 'Protocol error (Runtime.callFunctionOn): Target closed.'