mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-07 20:31:06 -05:00
Add cast for the fake TaggedError
This commit is contained in:
parent
fc197630be
commit
c44054a31b
2 changed files with 46 additions and 2 deletions
44
libraries/o-error/doc/benchmark.js
Normal file
44
libraries/o-error/doc/benchmark.js
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
//
|
||||||
|
// A quick microbenchmark for OError.tag.
|
||||||
|
//
|
||||||
|
const OError = require('..')
|
||||||
|
|
||||||
|
function benchmark(fn, repeats = 100000) {
|
||||||
|
const startTime = process.hrtime()
|
||||||
|
for (let i = 0; i < repeats; ++i) {
|
||||||
|
fn()
|
||||||
|
}
|
||||||
|
const elapsed = process.hrtime(startTime)
|
||||||
|
return elapsed[0] * 1e3 + elapsed[1] * 1e-6
|
||||||
|
}
|
||||||
|
|
||||||
|
function throwError() {
|
||||||
|
throw new Error('here is a test error')
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(
|
||||||
|
'no tagging: ',
|
||||||
|
benchmark(() => {
|
||||||
|
try {
|
||||||
|
throwError()
|
||||||
|
return 1
|
||||||
|
} catch (error) {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
'ms'
|
||||||
|
)
|
||||||
|
|
||||||
|
console.log(
|
||||||
|
'tagging: ',
|
||||||
|
benchmark(() => {
|
||||||
|
try {
|
||||||
|
throwError()
|
||||||
|
return 1
|
||||||
|
} catch (error) {
|
||||||
|
OError.tag(error, 'here is a test tag')
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
'ms'
|
||||||
|
)
|
|
@ -56,8 +56,8 @@ class OError extends Error {
|
||||||
|
|
||||||
let tag
|
let tag
|
||||||
if (Error.captureStackTrace) {
|
if (Error.captureStackTrace) {
|
||||||
// Hide this function in the stack trace.
|
// Hide this function in the stack trace, and avoid capturing it twice.
|
||||||
tag = { name: 'TaggedError', message, info }
|
tag = /** @type TaggedError */ ({ name: 'TaggedError', message, info })
|
||||||
Error.captureStackTrace(tag, OError.tag)
|
Error.captureStackTrace(tag, OError.tag)
|
||||||
} else {
|
} else {
|
||||||
tag = new TaggedError(message, info)
|
tag = new TaggedError(message, info)
|
||||||
|
|
Loading…
Reference in a new issue