Skip to content

Commit

Permalink
perf(errors): avoid parse error twice
Browse files Browse the repository at this point in the history
  • Loading branch information
Kikobeats committed Sep 12, 2024
1 parent 7b80314 commit 900174a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
20 changes: 18 additions & 2 deletions packages/errors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand All @@ -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 (
Expand Down
7 changes: 7 additions & 0 deletions packages/errors/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
Expand Down

0 comments on commit 900174a

Please sign in to comment.