UNPKG

1.22 kBJavaScriptView Raw
1const captureStack =
2 Error.captureStackTrace ||
3 function(error) {
4 const container = new Error()
5
6 Object.defineProperty(error, 'stack', {
7 configurable: true,
8 get() {
9 const { stack } = container
10
11 Object.defineProperty(this, 'stack', {
12 value: stack
13 })
14
15 return stack
16 }
17 })
18 }
19
20function inherits(ctor, superCtor) {
21 ctor.super_ = superCtor
22 ctor.prototype = Object.create(superCtor.prototype, {
23 constructor: {
24 value: ctor,
25 enumerable: false,
26 writable: true,
27 configurable: true
28 }
29 })
30}
31
32function ReamError(data) {
33 Object.defineProperty(this, 'name', {
34 configurable: true,
35 value: 'ReamError',
36 writable: true
37 })
38 for (const key of Object.keys(data)) {
39 this[key] = data[key]
40 }
41 inherits(this, Error)
42 captureStack(this)
43}
44
45export default ReamError
46
47// Following code only works without babel
48// Not sure why :(
49// export default class ReamError extends Error {
50// constructor({ message, code }) {
51// super(message)
52// this.name = 'ReamError'
53// this.code = code
54// if (Error.captureStackTrace) {
55// Error.captureStackTrace(this, this.constructor)
56// }
57// }
58// }