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 f12c80c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
20 changes: 19 additions & 1 deletion packages/errors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,22 @@ const whoops = require('whoops')

const ERROR_NAME = 'BrowserlessError'

const createBrowserlessError = opts => whoops(ERROR_NAME, opts)
const createErrorFactory = 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 +43,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 f12c80c

Please sign in to comment.