Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(errors): avoid parse error twice #592

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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