|
|
6 лет назад | |
|---|---|---|
| .. | ||
| .circleci | 6 лет назад | |
| test | 6 лет назад | |
| .editorconfig | 6 лет назад | |
| .eslintrc.json | 6 лет назад | |
| .gitignore | 8 лет назад | |
| .prettierrc.json | 6 лет назад | |
| LICENSE | 8 лет назад | |
| README.md | 6 лет назад | |
| http.js | 7 лет назад | |
| index.js | 7 лет назад | |
| package-lock.json | 6 лет назад | |
| package.json | 6 лет назад | |
Make custom error classes that:
instanceof checks,info), andES6 classes make it easy to define custom errors by subclassing Error. Subclassing OError adds a few extra helpers.
const OError = require('@overleaf/o-error')
function doSomethingBad () {
throw new OError({
message: 'did something bad',
info: { thing: 'foo' }
})
}
doSomethingBad()
// =>
// { OError: did something bad
// at doSomethingBad (repl:2:9) <-- stack trace
// name: 'OError', <-- default name
// info: { thing: 'foo' } } <-- attached info
class FooError extends OError {
constructor (options) {
super({ message: 'failed to foo', ...options })
}
}
function doFoo () {
throw new FooError({ info: { foo: 'bar' } })
}
doFoo()
// =>
// { FooError: failed to foo
// at doFoo (repl:2:9) <-- stack trace
// name: 'FooError', <-- correct name
// info: { foo: 'bar' } } <-- attached info
function doFoo2 () {
try {
throw new Error('bad')
} catch (err) {
throw new FooError({ info: { foo: 'bar' } }).withCause(err)
}
}
doFoo2()
// =>
// { FooError: failed to foo: bad <-- combined message
// at doFoo2 (repl:5:11) <-- stack trace
// name: 'FooError', <-- correct name
// info: { foo: 'bar' }, <-- attached info
// cause: <-- the cause (inner error)
// Error: bad <-- inner error message
// at doFoo2 (repl:3:11) <-- inner error stack trace
// at repl:1:1
// ...
try {
doFoo2()
} catch (err) {
console.log(OError.getFullStack(err))
}
// =>
// FooError: failed to foo: bad
// at doFoo2 (repl:5:11)
// at repl:2:3
// ...
// caused by: Error: bad
// at doFoo2 (repl:3:11)
// at repl:2:3
// ...