-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.js
38 lines (30 loc) · 1.01 KB
/
error.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
'use strict'
class BaseError extends Error {
// [error, ]code, message[, props]
constructor(...args) {
super()
if (args[0] instanceof Error)
this.cause = args.shift()
this.code = args.shift()
this.message = args.shift()
this.props = args.shift()
Error.captureStackTrace(this, this.constructor)
this.stack = this.stack.replace('\n', `\n ${this.code}: ${JSON.stringify(this.props || {})}\n`)
if (this.cause) {
const oldStack = Reflect.getOwnPropertyDescriptor(this, 'stack')
Reflect.defineProperty(this, 'stack', {
get: function() {
return oldStack.get.call(this) + '\nCaused by ' + this.cause.stack
}
})
}
if (!this.code)
throw new BaseError(this, 'error.code-missing', 'BaseError must be thrown with a code')
else if (!this.message)
throw new BaseError(this, 'error.message-missing', 'BaseError must be thrown with a message')
}
get name() {
return this.constructor.name
}
}
module.exports = BaseError