whoops 4.1.8 → 5.0.1
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/README.md +1 -0
- package/package.json +2 -7
- package/src/index.js +32 -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/README.md
CHANGED
|
@@ -11,6 +11,7 @@
|
|
|
11
11
|
- An easy way to create qualified errors.
|
|
12
12
|
- Using the standard `Error` interface in browser and NodeJS.
|
|
13
13
|
- Attach extra information, being flexible with whatever user case.
|
|
14
|
+
- Less than 50 lines (~500 bytes)
|
|
14
15
|
|
|
15
16
|
This library is a compromise to provide a clean API for use `Error` native class.
|
|
16
17
|
|
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.1",
|
|
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,38 @@
|
|
|
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)
|
|
8
|
+
const { message, ...props } = Object.assign(
|
|
9
|
+
{},
|
|
10
|
+
defaults,
|
|
11
|
+
typeof raw === 'string' ? { message: raw } : raw
|
|
12
|
+
)
|
|
13
|
+
Object.keys(props).forEach(key => (this[key] = props[key]))
|
|
14
|
+
if (message) this.description = typeof message === 'function' ? message(props) : message
|
|
15
|
+
this.message = this.code ? `${this.code}, ${this.description}` : this.description
|
|
16
|
+
this.name = name || ErrorClass.name
|
|
17
|
+
}
|
|
18
|
+
}
|
|
5
19
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
20
|
+
// Function to create an instance, allowing use without `new`
|
|
21
|
+
function CustomErrorFactory (props) {
|
|
22
|
+
return new CustomError(props)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// Ensure the function's name matches the class name for consistency
|
|
26
|
+
Object.defineProperty(CustomErrorFactory, 'name', {
|
|
27
|
+
value: name || ErrorClass.name,
|
|
28
|
+
writable: false
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
// Make `instanceof` checks work with both the class and factory
|
|
32
|
+
CustomErrorFactory.prototype = CustomError.prototype
|
|
33
|
+
|
|
34
|
+
return CustomErrorFactory
|
|
35
|
+
}
|
|
9
36
|
}
|
|
10
37
|
|
|
11
38
|
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
|
-
}
|