blob: 7ecc7a8016e5b706a5f8efc86824d53b29614e83 (
plain)
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
|
'use strict'
const {inherits} = require('./helpers')
const mimicFn = require('mimic-fn')
const REGEX_CLASS_NAME = /[^0-9a-zA-Z_$]/
function createError (className) {
if (typeof className !== 'string') {
throw new TypeError('Expected className to be a string')
}
if (REGEX_CLASS_NAME.test(className)) {
throw new Error('className contains invalid characters')
}
function ErrorClass () {
Object.defineProperty(this, 'name', {
configurable: true,
value: className,
writable: true
})
Error.captureStackTrace(this, this.constructor)
}
inherits(ErrorClass, Error)
mimicFn(ErrorClass, Error)
return ErrorClass
}
module.exports = createError
|