index.js
3.33 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// ISC @ Julien Fontanet
'use strict'
// ===================================================================
var defineProperty = Object.defineProperty
// -------------------------------------------------------------------
var captureStackTrace = Error.captureStackTrace
if (!captureStackTrace) {
captureStackTrace = function captureStackTrace (error) {
var container = new Error()
defineProperty(error, 'stack', {
configurable: true,
get: function getStack () {
var stack = container.stack
// Replace property with value for faster future accesses.
defineProperty(this, 'stack', {
value: stack
})
return stack
},
set: function setStack (stack) {
defineProperty(error, 'stack', {
configurable: true,
value: stack,
writable: true
})
}
})
}
}
// -------------------------------------------------------------------
function BaseError (message) {
if (message) {
defineProperty(this, 'message', {
configurable: true,
value: message,
writable: true
})
}
var cname = this.constructor.name
if (
cname &&
cname !== this.name
) {
defineProperty(this, 'name', {
configurable: true,
value: cname,
writable: true
})
}
captureStackTrace(this, this.constructor)
}
BaseError.prototype = Object.create(Error.prototype, {
// See: https://github.com/JsCommunity/make-error/issues/4
constructor: {
configurable: true,
value: BaseError,
writable: true
}
})
// -------------------------------------------------------------------
// Sets the name of a function if possible (depends of the JS engine).
var setFunctionName = (function () {
function setFunctionName (fn, name) {
return defineProperty(fn, 'name', {
configurable: true,
value: name
})
}
try {
var f = function () {}
setFunctionName(f, 'foo')
if (f.name === 'foo') {
return setFunctionName
}
} catch (_) {}
})()
// -------------------------------------------------------------------
function makeError (constructor, super_) {
if (super_ == null || super_ === Error) {
super_ = BaseError
} else if (typeof super_ !== 'function') {
throw new TypeError('super_ should be a function')
}
var name
if (typeof constructor === 'string') {
name = constructor
constructor = function () { super_.apply(this, arguments) }
// If the name can be set, do it once and for all.
if (setFunctionName) {
setFunctionName(constructor, name)
name = null
}
} else if (typeof constructor !== 'function') {
throw new TypeError('constructor should be either a string or a function')
}
// Also register the super constructor also as `constructor.super_` just
// like Node's `util.inherits()`.
constructor.super_ = constructor['super'] = super_
var properties = {
constructor: {
configurable: true,
value: constructor,
writable: true
}
}
// If the name could not be set on the constructor, set it on the
// prototype.
if (name != null) {
properties.name = {
configurable: true,
value: name,
writable: true
}
}
constructor.prototype = Object.create(super_.prototype, properties)
return constructor
}
exports = module.exports = makeError
exports.BaseError = BaseError