whoops 4.1.8 → 5.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -7
- package/src/index.js +31 -5
- package/src/add-error-props.js +0 -15
- package/src/create-error.js +0 -34
- package/src/create-extend-error.js +0 -25
- package/src/helpers.js +0 -18
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"name": "whoops",
|
|
3
3
|
"description": "It makes simple throw qualified errors.",
|
|
4
4
|
"homepage": "https://github.com/Kikobeats/whoops",
|
|
5
|
-
"version": "
|
|
5
|
+
"version": "5.0.0",
|
|
6
6
|
"main": "src/index.js",
|
|
7
7
|
"author": {
|
|
8
8
|
"email": "josefrancisco.verdu@gmail.com",
|
|
@@ -33,10 +33,6 @@
|
|
|
33
33
|
"simple",
|
|
34
34
|
"throw"
|
|
35
35
|
],
|
|
36
|
-
"dependencies": {
|
|
37
|
-
"clean-stack": "~3.0.0",
|
|
38
|
-
"mimic-fn": "~3.1.0"
|
|
39
|
-
},
|
|
40
36
|
"devDependencies": {
|
|
41
37
|
"@commitlint/cli": "latest",
|
|
42
38
|
"@commitlint/config-conventional": "latest",
|
|
@@ -46,7 +42,6 @@
|
|
|
46
42
|
"finepack": "latest",
|
|
47
43
|
"git-authors-cli": "latest",
|
|
48
44
|
"github-generate-release": "latest",
|
|
49
|
-
"mocha": "latest",
|
|
50
45
|
"nano-staged": "latest",
|
|
51
46
|
"should": "latest",
|
|
52
47
|
"simple-git-hooks": "latest",
|
|
@@ -70,7 +65,7 @@
|
|
|
70
65
|
"release": "standard-version -a",
|
|
71
66
|
"release:github": "github-generate-release",
|
|
72
67
|
"release:tags": "git push --follow-tags origin HEAD:master",
|
|
73
|
-
"test": "c8
|
|
68
|
+
"test": "c8 node --test"
|
|
74
69
|
},
|
|
75
70
|
"license": "MIT",
|
|
76
71
|
"commitlint": {
|
package/src/index.js
CHANGED
|
@@ -1,11 +1,37 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
function createErrorClass (ErrorClass) {
|
|
4
|
+
return (name, defaults) => {
|
|
5
|
+
class CustomError extends ErrorClass {
|
|
6
|
+
constructor (raw = {}) {
|
|
7
|
+
super(raw)
|
|
5
8
|
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
|
|
9
|
+
const { message, ...props } = typeof raw === 'string' ? { message: raw } : raw
|
|
10
|
+
const mergedProps = Object.assign({}, defaults, props)
|
|
11
|
+
Object.keys(mergedProps).forEach(key => (this[key] = mergedProps[key]))
|
|
12
|
+
|
|
13
|
+
if (message) this.description = typeof message === 'function' ? message(this) : message
|
|
14
|
+
this.message = this.code ? `${this.code}, ${this.description}` : this.description
|
|
15
|
+
this.name = name || ErrorClass.name
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// Function to create an instance, allowing use without `new`
|
|
20
|
+
function CustomErrorFactory (props) {
|
|
21
|
+
return new CustomError(props)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
// Ensure the function's name matches the class name for consistency
|
|
25
|
+
Object.defineProperty(CustomErrorFactory, 'name', {
|
|
26
|
+
value: name || ErrorClass.name,
|
|
27
|
+
writable: false
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
// Make `instanceof` checks work with both the class and factory
|
|
31
|
+
CustomErrorFactory.prototype = CustomError.prototype
|
|
32
|
+
|
|
33
|
+
return CustomErrorFactory
|
|
34
|
+
}
|
|
9
35
|
}
|
|
10
36
|
|
|
11
37
|
module.exports = createErrorClass(Error)
|
package/src/add-error-props.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { isFunction, composeErrorMessage } = require('./helpers')
|
|
4
|
-
|
|
5
|
-
function interfaceObject (error, ...props) {
|
|
6
|
-
Object.assign(error, ...props)
|
|
7
|
-
|
|
8
|
-
error.description = isFunction(error.message) ? error.message(error) : error.message
|
|
9
|
-
|
|
10
|
-
error.message = error.code
|
|
11
|
-
? composeErrorMessage(error.code, error.description)
|
|
12
|
-
: error.description
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
module.exports = interfaceObject
|
package/src/create-error.js
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const { inherits } = require('./helpers')
|
|
4
|
-
const mimicFn = require('mimic-fn')
|
|
5
|
-
|
|
6
|
-
const REGEX_CLASS_NAME = /[^0-9a-zA-Z_$]/
|
|
7
|
-
|
|
8
|
-
function createError (className) {
|
|
9
|
-
if (typeof className !== 'string') {
|
|
10
|
-
throw new TypeError('Expected className to be a string')
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
if (REGEX_CLASS_NAME.test(className)) {
|
|
14
|
-
throw new Error('className contains invalid characters')
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
function ErrorClass () {
|
|
18
|
-
Object.defineProperty(this, 'name', {
|
|
19
|
-
configurable: true,
|
|
20
|
-
value: className,
|
|
21
|
-
writable: true
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
if ('captureStackTrace' in Error) {
|
|
25
|
-
Error.captureStackTrace(this, this.constructor)
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
inherits(ErrorClass, Error)
|
|
30
|
-
mimicFn(ErrorClass, Error)
|
|
31
|
-
return ErrorClass
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
module.exports = createError
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
const cleanStack = require('clean-stack')
|
|
4
|
-
const mimicFn = require('mimic-fn')
|
|
5
|
-
|
|
6
|
-
const addErrorProps = require('./add-error-props')
|
|
7
|
-
const { isString } = require('./helpers')
|
|
8
|
-
|
|
9
|
-
function createExtendError (ErrorClass, classProps) {
|
|
10
|
-
function ExtendError (props) {
|
|
11
|
-
const error = new ErrorClass()
|
|
12
|
-
const errorProps = isString(props) ? { message: props } : props
|
|
13
|
-
addErrorProps(error, classProps, errorProps)
|
|
14
|
-
|
|
15
|
-
error.stack = typeof error.stack === 'string' ? cleanStack(error.stack) : undefined
|
|
16
|
-
return error
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
ExtendError.prototype = ErrorClass.prototype
|
|
20
|
-
mimicFn(ExtendError, ErrorClass)
|
|
21
|
-
|
|
22
|
-
return ExtendError
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
module.exports = createExtendError
|
package/src/helpers.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
'use strict'
|
|
2
|
-
|
|
3
|
-
module.exports = {
|
|
4
|
-
isFunction: obj => typeof obj === 'function',
|
|
5
|
-
isString: obj => typeof obj === 'string',
|
|
6
|
-
composeErrorMessage: (code, description) => `${code}, ${description}`,
|
|
7
|
-
inherits: (ctor, superCtor) => {
|
|
8
|
-
ctor.super_ = superCtor
|
|
9
|
-
ctor.prototype = Object.create(superCtor.prototype, {
|
|
10
|
-
constructor: {
|
|
11
|
-
value: ctor,
|
|
12
|
-
enumerable: false,
|
|
13
|
-
writable: true,
|
|
14
|
-
configurable: true
|
|
15
|
-
}
|
|
16
|
-
})
|
|
17
|
-
}
|
|
18
|
-
}
|