immediate-error 6.4.0 → 7.1.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/README.md +0 -6
- package/index.d.ts +6 -7
- package/index.js +33 -32
- package/index.test.js +4 -9
- package/package.json +5 -7
package/README.md
CHANGED
|
@@ -19,12 +19,6 @@ immediateError('Aaaaah') // this will throw a regular Error with the message "Aa
|
|
|
19
19
|
|
|
20
20
|
immediateError('Aaaaah', ErrorType.BaseError) // does the same thing as above
|
|
21
21
|
|
|
22
|
-
immediateError('Aggregate error', ErrorType.AggregateError) // throws an AggregateError
|
|
23
|
-
|
|
24
|
-
immediateError('Assertion error', ErrorType.AssertionError) // throws an AssertionError (from the assert-fn module)
|
|
25
|
-
|
|
26
|
-
immediateError('Assertion error', ErrorType.NativeAssertionError) // throws an AssertionError (from the node:assert module)
|
|
27
|
-
|
|
28
22
|
immediateError('Range error', ErrorType.RangeError) // throws a RangeError
|
|
29
23
|
|
|
30
24
|
immediateError('Reference error', ErrorType.ReferenceError) // throws a ReferenceError
|
package/index.d.ts
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
export enum ErrorType {
|
|
2
2
|
BaseError = 0,
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
NativeAssertionError = 7
|
|
3
|
+
EvalError = 1,
|
|
4
|
+
RangeError = 2,
|
|
5
|
+
ReferenceError = 3,
|
|
6
|
+
SyntaxError = 4,
|
|
7
|
+
TypeError = 5,
|
|
8
|
+
URIError = 6
|
|
10
9
|
}
|
|
11
10
|
|
|
12
11
|
export type CustomError = {
|
package/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const GetIntrinsic = require("
|
|
1
|
+
const GetIntrinsic = require("es-intrinsic-cache")
|
|
2
2
|
const $Object = require("es-object-atoms")
|
|
3
3
|
const zero = require("@positive-numbers/zero")
|
|
4
4
|
const one = require("@positive-numbers/one")
|
|
@@ -7,35 +7,34 @@ const three = require("@positive-numbers/three")
|
|
|
7
7
|
const four = require("@positive-numbers/four")
|
|
8
8
|
const five = require("@positive-numbers/five")
|
|
9
9
|
const six = require("@positive-numbers/six")
|
|
10
|
-
const
|
|
11
|
-
const
|
|
10
|
+
const noop = require("yanoop").noop
|
|
11
|
+
const bail = require("bail")
|
|
12
12
|
const { Switch } = require("switch-in-fp")
|
|
13
|
-
const assert = require("assert-fn")
|
|
14
|
-
const nativeAssert = require("node:assert")
|
|
15
13
|
const vm = require("node:vm")
|
|
16
14
|
const construct = require("construct-new")
|
|
17
15
|
const attempt = require("attempt-statement")
|
|
16
|
+
const trueValue = require("true-value")
|
|
18
17
|
|
|
19
|
-
const $BaseError = require("es-
|
|
20
|
-
const $
|
|
21
|
-
const $
|
|
22
|
-
const $
|
|
23
|
-
const $
|
|
24
|
-
const $
|
|
25
|
-
const $
|
|
26
|
-
|
|
18
|
+
const $BaseError = require("es-error-intrinsics/Error")
|
|
19
|
+
const $EvalError = require("es-error-intrinsics/EvalError")
|
|
20
|
+
const $RangeError = require("es-error-intrinsics/RangeError")
|
|
21
|
+
const $ReferenceError = require("es-error-intrinsics/ReferenceError")
|
|
22
|
+
const $SyntaxError = require("es-error-intrinsics/SyntaxError")
|
|
23
|
+
const $TypeError = require("es-error-intrinsics/TypeError")
|
|
24
|
+
const $URIError = require("es-error-intrinsics/URIError")
|
|
25
|
+
|
|
26
|
+
const captureStackTrace = GetIntrinsic("%Error.captureStackTrace%", trueValue())
|
|
27
27
|
|
|
28
28
|
const default_error = "ERROR!"
|
|
29
29
|
|
|
30
30
|
const ErrorType = $Object.freeze({
|
|
31
31
|
BaseError: zero,
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
NativeAssertionError: seven
|
|
32
|
+
EvalError: one,
|
|
33
|
+
RangeError: two,
|
|
34
|
+
ReferenceError: three,
|
|
35
|
+
SyntaxError: four,
|
|
36
|
+
TypeError: five,
|
|
37
|
+
URIError: six,
|
|
39
38
|
})
|
|
40
39
|
|
|
41
40
|
exports.immediateError = function immediateError(message = default_error, errorType = ErrorType.BaseError) {
|
|
@@ -45,11 +44,8 @@ exports.immediateError = function immediateError(message = default_error, errorT
|
|
|
45
44
|
.case(ErrorType.BaseError, function () {
|
|
46
45
|
error = construct({ target: $BaseError, args: [message] })
|
|
47
46
|
})
|
|
48
|
-
.case(ErrorType.
|
|
49
|
-
error = construct({ target: $
|
|
50
|
-
})
|
|
51
|
-
.case(ErrorType.AggregateError, function () {
|
|
52
|
-
error = construct({ target: $AggregateError, args: [message] })
|
|
47
|
+
.case(ErrorType.EvalError, function () {
|
|
48
|
+
error = construct({ target: $EvalError, args: [message] })
|
|
53
49
|
})
|
|
54
50
|
.case(ErrorType.RangeError, function () {
|
|
55
51
|
error = construct({ target: $RangeError, args: [message] })
|
|
@@ -63,25 +59,30 @@ exports.immediateError = function immediateError(message = default_error, errorT
|
|
|
63
59
|
.case(ErrorType.TypeError, function () {
|
|
64
60
|
error = construct({ target: $TypeError, args: [message] })
|
|
65
61
|
})
|
|
66
|
-
.case(ErrorType.
|
|
67
|
-
error = construct({ target: $
|
|
62
|
+
.case(ErrorType.URIError, function () {
|
|
63
|
+
error = construct({ target: $URIError, args: [message] })
|
|
68
64
|
})
|
|
69
65
|
.else(function () {
|
|
70
|
-
attempt(function() {
|
|
66
|
+
attempt(function () {
|
|
71
67
|
error = construct({ target: errorType, args: [message] })
|
|
72
|
-
}).rescue(function() {
|
|
68
|
+
}).rescue(function () {
|
|
73
69
|
error = construct({ target: $BaseError, args: [message] })
|
|
74
70
|
}).else(noop).ensure(noop).end()
|
|
75
71
|
})
|
|
76
72
|
.execute()
|
|
77
73
|
|
|
74
|
+
if (captureStackTrace) {
|
|
75
|
+
captureStackTrace(error, immediateError)
|
|
76
|
+
}
|
|
77
|
+
|
|
78
78
|
const context = {
|
|
79
|
-
error,
|
|
80
|
-
|
|
79
|
+
error: error,
|
|
80
|
+
bail: bail
|
|
81
81
|
}
|
|
82
|
+
|
|
82
83
|
vm.createContext(context)
|
|
83
84
|
|
|
84
|
-
const script = construct({ target: vm.Script, args: [`
|
|
85
|
+
const script = construct({ target: vm.Script, args: [`bail(error)`, { filename: default_error }] })
|
|
85
86
|
|
|
86
87
|
script.runInContext(context)
|
|
87
88
|
}
|
package/index.test.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const { immediateError, ErrorType } = require('
|
|
1
|
+
const { immediateError, ErrorType } = require('./index')
|
|
2
2
|
const assert = require('node:assert')
|
|
3
3
|
|
|
4
4
|
describe('immediateError utility', () => {
|
|
@@ -16,21 +16,16 @@ describe('immediateError utility', () => {
|
|
|
16
16
|
|
|
17
17
|
// Native Error Types
|
|
18
18
|
test.each([
|
|
19
|
+
['BaseError', ErrorType.BaseError, Error],
|
|
20
|
+
['EvalError', ErrorType.EvalError, EvalError],
|
|
19
21
|
['RangeError', ErrorType.RangeError, RangeError],
|
|
20
22
|
['ReferenceError', ErrorType.ReferenceError, ReferenceError],
|
|
21
23
|
['SyntaxError', ErrorType.SyntaxError, SyntaxError],
|
|
22
24
|
['TypeError', ErrorType.TypeError, TypeError],
|
|
23
|
-
['
|
|
25
|
+
['RangeError', ErrorType.URIError, URIError],
|
|
24
26
|
])('throws %s when specified', (name, type, constructor) => {
|
|
25
27
|
expect(() => immediateError('test message', type)).toThrow(constructor)
|
|
26
28
|
})
|
|
27
|
-
|
|
28
|
-
// Assertion Errors
|
|
29
|
-
test('throws NativeAssertionError (node:assert)', () => {
|
|
30
|
-
expect(() => immediateError('failed', ErrorType.NativeAssertionError))
|
|
31
|
-
.toThrow(assert.AssertionError)
|
|
32
|
-
})
|
|
33
|
-
|
|
34
29
|
// Custom Error Classes
|
|
35
30
|
test('throws a custom user-defined Error class', () => {
|
|
36
31
|
class MyCustomError extends Error {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "immediate-error",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "7.1.0",
|
|
4
4
|
"description": "throw errors in fp",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -26,7 +26,6 @@
|
|
|
26
26
|
"libvegetable",
|
|
27
27
|
"bail",
|
|
28
28
|
"throw-error",
|
|
29
|
-
"fox",
|
|
30
29
|
"sigma",
|
|
31
30
|
"lifesaver",
|
|
32
31
|
"framework",
|
|
@@ -34,7 +33,6 @@
|
|
|
34
33
|
"ecmascript",
|
|
35
34
|
"javascript",
|
|
36
35
|
"licensed",
|
|
37
|
-
"unlicensed-licensed-license",
|
|
38
36
|
"jest",
|
|
39
37
|
"67",
|
|
40
38
|
"fp",
|
|
@@ -54,19 +52,19 @@
|
|
|
54
52
|
"@positive-numbers/five": "^3.0.0",
|
|
55
53
|
"@positive-numbers/four": "^3.0.0",
|
|
56
54
|
"@positive-numbers/one": "^3.0.0",
|
|
57
|
-
"@positive-numbers/seven": "^3.0.0",
|
|
58
55
|
"@positive-numbers/six": "^3.0.0",
|
|
59
56
|
"@positive-numbers/three": "^3.0.0",
|
|
60
57
|
"@positive-numbers/two": "^3.0.0",
|
|
61
58
|
"@positive-numbers/zero": "^3.0.0",
|
|
62
|
-
"assert-fn": "^1.0.1",
|
|
63
59
|
"attempt-statement": "^1.2.0",
|
|
60
|
+
"bail": "^1.0.5",
|
|
64
61
|
"construct-new": "^2.0.3",
|
|
65
|
-
"es-
|
|
62
|
+
"es-error-intrinsics": "^1.0.1",
|
|
63
|
+
"es-intrinsic-cache": "^1.0.1",
|
|
66
64
|
"es-object-atoms": "^1.1.1",
|
|
67
|
-
"get-intrinsic": "^1.3.1",
|
|
68
65
|
"number-zero": "^1.0.3",
|
|
69
66
|
"switch-in-fp": "^3.0.0",
|
|
67
|
+
"true-value": "^2.0.5",
|
|
70
68
|
"yanoop": "^1.0.0"
|
|
71
69
|
},
|
|
72
70
|
"devDependencies": {
|