|
|
@@ -1,12 +1,104 @@
|
|
|
# overleaf-error-type
|
|
|
|
|
|
-Make custom error types that pass `instanceof` checks, have stack traces and support custom messages and properties.
|
|
|
+Make custom error types that:
|
|
|
+- 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).
|
|
|
+
|
|
|
+## For ES6
|
|
|
+
|
|
|
+ES6 classes make it easy to define custom errors by subclassing `Error`. Subclassing `errorType.Error` adds a few extra helpers.
|
|
|
+
|
|
|
+### Usage
|
|
|
+
|
|
|
+#### Throw an error directly
|
|
|
+
|
|
|
+```js
|
|
|
+const errorType = require('overleaf-error-type')
|
|
|
+
|
|
|
+function doSomethingBad () {
|
|
|
+ throw new errorType.Error({
|
|
|
+ message: 'did something bad',
|
|
|
+ info: { thing: 'foo' }
|
|
|
+ })
|
|
|
+}
|
|
|
+doSomethingBad()
|
|
|
+// =>
|
|
|
+// { ErrorTypeError: did something bad
|
|
|
+// at doSomethingBad (repl:2:9) <-- stack trace
|
|
|
+// name: 'ErrorTypeError', <-- default name
|
|
|
+// info: { thing: 'foo' } } <-- attached info
|
|
|
+```
|
|
|
+
|
|
|
+#### Custom error class
|
|
|
+
|
|
|
+```js
|
|
|
+class FooError extends errorType.Error {
|
|
|
+ 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
|
|
|
+```
|
|
|
+
|
|
|
+#### Wrapping an inner error (cause)
|
|
|
+
|
|
|
+```js
|
|
|
+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(errorType.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
|
|
|
+// ...
|
|
|
+```
|
|
|
+
|
|
|
+## For ES5
|
|
|
+
|
|
|
+For backward compatibility, the following ES5-only interface is still supported.
|
|
|
|
|
|
The approach is based mainly on https://gist.github.com/justmoon/15511f92e5216fa2624b; it just tries to DRY it up a bit.
|
|
|
|
|
|
-## Usage
|
|
|
+### Usage
|
|
|
|
|
|
-### Define a standalone error class
|
|
|
+#### Define a standalone error class
|
|
|
|
|
|
```js
|
|
|
const errorType = require('overleaf-error-type')
|
|
|
@@ -22,7 +114,7 @@ doSomethingBad()
|
|
|
// at doSomethingBad (repl:2:9) <-- stack trace
|
|
|
```
|
|
|
|
|
|
-### Define an error subclass
|
|
|
+#### Define an error subclass
|
|
|
|
|
|
```js
|
|
|
const SubCustomError = errorType.extend(CustomError, 'SubCustomError')
|
|
|
@@ -37,7 +129,7 @@ try {
|
|
|
}
|
|
|
```
|
|
|
|
|
|
-### Add custom message and/or properties
|
|
|
+#### Add custom message and/or properties
|
|
|
|
|
|
```js
|
|
|
const UserNotFoundError = errorType.define('UserNotFoundError',
|
|
|
@@ -50,7 +142,7 @@ throw new UserNotFoundError(123)
|
|
|
// => UserNotFoundError: User not found: 123
|
|
|
```
|
|
|
|
|
|
-### Add custom Error types under an existing class
|
|
|
+#### Add custom Error types under an existing class
|
|
|
|
|
|
```js
|
|
|
class User {
|
|
|
@@ -72,5 +164,17 @@ User.lookup(123)
|
|
|
|
|
|
## References
|
|
|
|
|
|
+General:
|
|
|
+
|
|
|
+- [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)
|
|
|
+
|
|
|
+For ES6:
|
|
|
+
|
|
|
+- [Custom JavaScript Errors in ES6](https://medium.com/@xjamundx/custom-javascript-errors-in-es6-aa891b173f87)
|
|
|
+- [Custom errors, extending Error](https://javascript.info/custom-errors)
|
|
|
+
|
|
|
+For ES5:
|
|
|
+
|
|
|
- https://gist.github.com/justmoon/15511f92e5216fa2624b
|
|
|
-- [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error)
|