overleaf/libraries/o-error/README.md

102 lines
2.7 KiB
Markdown
Raw Normal View History

2019-07-08 06:45:41 -04:00
# @overleaf/o-error
2018-04-21 18:24:23 -04:00
2020-04-17 04:13:04 -04:00
[![CircleCI](https://circleci.com/gh/overleaf/o-error.svg?style=svg)](https://circleci.com/gh/overleaf/o-error)
2019-07-08 06:45:41 -04:00
Make custom error classes that:
2020-04-17 03:44:50 -04:00
- pass `instanceof` checks,
- have stack traces,
- support custom messages and properties (`info`), and
- can wrap internal errors (causes) like [VError](https://github.com/joyent/node-verror).
2019-07-05 12:45:43 -04:00
ES6 classes make it easy to define custom errors by subclassing `Error`. Subclassing `OError` adds a few extra helpers.
2019-07-08 06:45:41 -04:00
## Usage
2019-07-08 06:45:41 -04:00
### Throw an error directly
```js
2019-07-08 06:45:41 -04:00
const OError = require('@overleaf/o-error')
2020-04-17 03:44:50 -04:00
function doSomethingBad() {
2019-07-05 12:45:43 -04:00
throw new OError({
message: 'did something bad',
2020-04-17 03:44:50 -04:00
info: { thing: 'foo' },
})
}
doSomethingBad()
// =>
2019-07-08 06:45:41 -04:00
// { OError: did something bad
// at doSomethingBad (repl:2:9) <-- stack trace
2019-07-08 06:45:41 -04:00
// name: 'OError', <-- default name
// info: { thing: 'foo' } } <-- attached info
```
2019-07-08 06:45:41 -04:00
### Custom error class
```js
2019-07-05 12:45:43 -04:00
class FooError extends OError {
2020-04-17 03:44:50 -04:00
constructor(options) {
super({ message: 'failed to foo', ...options })
}
}
2020-04-17 03:44:50 -04:00
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
```
2019-07-08 06:45:41 -04:00
### Wrapping an inner error (cause)
```js
2020-04-17 03:44:50 -04:00
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) {
2019-07-05 12:45:43 -04:00
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
// ...
```
2018-04-21 18:24:23 -04:00
## References
- [MDN: Error](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)
- [Error Handling in Node.js](https://www.joyent.com/node-js/production/design/errors)
- [verror](https://github.com/joyent/node-verror)
- [Custom JavaScript Errors in ES6](https://medium.com/@xjamundx/custom-javascript-errors-in-es6-aa891b173f87)
- [Custom errors, extending Error](https://javascript.info/custom-errors)
2019-07-08 06:45:41 -04:00
- https://gist.github.com/justmoon/15511f92e5216fa2624b (some tests are based largely on this gist)