immediate-error 6.3.0 → 7.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/README.md +11 -19
- package/index.d.ts +14 -8
- package/index.js +75 -94
- package/index.test.js +41 -0
- package/package.json +52 -22
package/README.md
CHANGED
|
@@ -1,6 +1,13 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `immediate-error`
|
|
2
2
|
|
|
3
|
-
This is a utility to throw an error.
|
|
3
|
+
This is a utility to throw an error in FP.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
```bash
|
|
7
|
+
$ npm install immediate-error
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
## Usage
|
|
4
11
|
|
|
5
12
|
```javascript
|
|
6
13
|
|
|
@@ -12,12 +19,6 @@ immediateError('Aaaaah') // this will throw a regular Error with the message "Aa
|
|
|
12
19
|
|
|
13
20
|
immediateError('Aaaaah', ErrorType.BaseError) // does the same thing as above
|
|
14
21
|
|
|
15
|
-
immediateError('Aggregate error', ErrorType.AggregateError) // throws an AggregateError
|
|
16
|
-
|
|
17
|
-
immediateError('Assertion error', ErrorType.AssertionError) // throws an AssertionError (from the assert-fn module)
|
|
18
|
-
|
|
19
|
-
immediateError('Assertion error', ErrorType.NativeAssertionError) // throws an AssertionError (from the node:assert module)
|
|
20
|
-
|
|
21
22
|
immediateError('Range error', ErrorType.RangeError) // throws a RangeError
|
|
22
23
|
|
|
23
24
|
immediateError('Reference error', ErrorType.ReferenceError) // throws a ReferenceError
|
|
@@ -35,14 +36,5 @@ class MyCustomError extends Error {
|
|
|
35
36
|
immediateError('Error!', MyCustomError) // throws a MyCustomError with the message "Error!" which in turn prints out "Custom: Error!" because we used our own error class
|
|
36
37
|
|
|
37
38
|
```
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
This module is great because it uses the best practices in JavaScript such as using dependencies like [`yanoop`](https://npmjs.com/package/yanoop) to throw errors and using [`es-errors`](https://npmjs.com/package/es-errors) instead of using the error classes natively.
|
|
42
|
-
|
|
43
|
-
## why?
|
|
44
|
-
why not
|
|
45
|
-
|
|
46
|
-
## Show your support
|
|
47
|
-
|
|
48
|
-
[Follow me on GitHub](https://github.com/tj-commits) and star my repositories.
|
|
39
|
+
## License
|
|
40
|
+
Unlicense
|
package/index.d.ts
CHANGED
|
@@ -1,12 +1,18 @@
|
|
|
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
|
-
export
|
|
11
|
+
export type CustomError = {
|
|
12
|
+
new (message: string): never
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function immediateError(
|
|
16
|
+
message: string,
|
|
17
|
+
errorType?: ErrorType | CustomError
|
|
18
|
+
): never
|
package/index.js
CHANGED
|
@@ -1,109 +1,90 @@
|
|
|
1
|
-
require(
|
|
2
|
-
require(
|
|
3
|
-
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
const
|
|
15
|
-
const
|
|
16
|
-
const
|
|
17
|
-
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
const $
|
|
21
|
-
const $
|
|
22
|
-
const $
|
|
23
|
-
const $
|
|
24
|
-
const $
|
|
25
|
-
|
|
26
|
-
const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
const ERROR = Object.freeze({
|
|
1
|
+
const GetIntrinsic = require("get-intrinsic")
|
|
2
|
+
const $Object = require("es-object-atoms")
|
|
3
|
+
const zero = require("@positive-numbers/zero")
|
|
4
|
+
const one = require("@positive-numbers/one")
|
|
5
|
+
const two = require("@positive-numbers/two")
|
|
6
|
+
const three = require("@positive-numbers/three")
|
|
7
|
+
const four = require("@positive-numbers/four")
|
|
8
|
+
const five = require("@positive-numbers/five")
|
|
9
|
+
const six = require("@positive-numbers/six")
|
|
10
|
+
const noop = require("yanoop").noop
|
|
11
|
+
const bail = require("bail")
|
|
12
|
+
const { Switch } = require("switch-in-fp")
|
|
13
|
+
const vm = require("node:vm")
|
|
14
|
+
const construct = require("construct-new")
|
|
15
|
+
const attempt = require("attempt-statement")
|
|
16
|
+
const trueValue = require("true-value")
|
|
17
|
+
|
|
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
|
+
|
|
28
|
+
const default_error = "ERROR!"
|
|
29
|
+
|
|
30
|
+
const ErrorType = $Object.freeze({
|
|
32
31
|
BaseError: zero,
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
NativeAssertionError: seven()
|
|
32
|
+
EvalError: one,
|
|
33
|
+
RangeError: two,
|
|
34
|
+
ReferenceError: three,
|
|
35
|
+
SyntaxError: four,
|
|
36
|
+
TypeError: five,
|
|
37
|
+
URIError: six,
|
|
40
38
|
})
|
|
41
39
|
|
|
42
|
-
exports.immediateError = function immediateError(message = default_error, errorType =
|
|
40
|
+
exports.immediateError = function immediateError(message = default_error, errorType = ErrorType.BaseError) {
|
|
43
41
|
var error
|
|
44
42
|
|
|
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
|
-
error = new $TypeError(message)
|
|
79
|
-
break
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
case ERROR.NativeAssertionError: {
|
|
83
|
-
error = new $NativeAssertionError(message)
|
|
84
|
-
break
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
default: {
|
|
88
|
-
try {
|
|
89
|
-
error = new errorType(message)
|
|
90
|
-
} catch (err) {
|
|
91
|
-
[err] // put the error behind bars, where it belongs
|
|
92
|
-
error = new $BaseError(message)
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
}
|
|
43
|
+
Switch(errorType)
|
|
44
|
+
.case(ErrorType.BaseError, function () {
|
|
45
|
+
error = construct({ target: $BaseError, args: [message] })
|
|
46
|
+
})
|
|
47
|
+
.case(ErrorType.EvalError, function () {
|
|
48
|
+
error = construct({ target: $EvalError, args: [message] })
|
|
49
|
+
})
|
|
50
|
+
.case(ErrorType.RangeError, function () {
|
|
51
|
+
error = construct({ target: $RangeError, args: [message] })
|
|
52
|
+
})
|
|
53
|
+
.case(ErrorType.ReferenceError, function () {
|
|
54
|
+
error = construct({ target: $ReferenceError, args: [message] })
|
|
55
|
+
})
|
|
56
|
+
.case(ErrorType.SyntaxError, function () {
|
|
57
|
+
error = construct({ target: $SyntaxError, args: [message] })
|
|
58
|
+
})
|
|
59
|
+
.case(ErrorType.TypeError, function () {
|
|
60
|
+
error = construct({ target: $TypeError, args: [message] })
|
|
61
|
+
})
|
|
62
|
+
.case(ErrorType.URIError, function () {
|
|
63
|
+
error = construct({ target: $URIError, args: [message] })
|
|
64
|
+
})
|
|
65
|
+
.else(function () {
|
|
66
|
+
attempt(function () {
|
|
67
|
+
error = construct({ target: errorType, args: [message] })
|
|
68
|
+
}).rescue(function () {
|
|
69
|
+
error = construct({ target: $BaseError, args: [message] })
|
|
70
|
+
}).else(noop).ensure(noop).end()
|
|
71
|
+
})
|
|
72
|
+
.execute()
|
|
73
|
+
|
|
74
|
+
if (captureStackTrace) {
|
|
75
|
+
captureStackTrace(error, immediateError)
|
|
96
76
|
}
|
|
97
77
|
|
|
98
78
|
const context = {
|
|
99
|
-
error,
|
|
100
|
-
|
|
79
|
+
error: error,
|
|
80
|
+
bail: bail
|
|
101
81
|
}
|
|
82
|
+
|
|
102
83
|
vm.createContext(context)
|
|
103
84
|
|
|
104
|
-
const script =
|
|
85
|
+
const script = construct({ target: vm.Script, args: [`bail(error)`, { filename: default_error }] })
|
|
105
86
|
|
|
106
87
|
script.runInContext(context)
|
|
107
88
|
}
|
|
108
89
|
|
|
109
|
-
exports.ErrorType =
|
|
90
|
+
exports.ErrorType = ErrorType
|
package/index.test.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
const { immediateError, ErrorType } = require('./index')
|
|
2
|
+
const assert = require('node:assert')
|
|
3
|
+
|
|
4
|
+
describe('immediateError utility', () => {
|
|
5
|
+
|
|
6
|
+
// Basic Usage
|
|
7
|
+
test('throws a regular Error with default message when no arguments are passed', () => {
|
|
8
|
+
expect(() => immediateError()).toThrow(Error)
|
|
9
|
+
expect(() => immediateError()).toThrow('ERROR!')
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
test('throws a regular Error with a custom message', () => {
|
|
13
|
+
expect(() => immediateError('Aaaaah')).toThrow(Error)
|
|
14
|
+
expect(() => immediateError('Aaaaah')).toThrow('Aaaaah')
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
// Native Error Types
|
|
18
|
+
test.each([
|
|
19
|
+
['BaseError', ErrorType.BaseError, Error],
|
|
20
|
+
['EvalError', ErrorType.EvalError, EvalError],
|
|
21
|
+
['RangeError', ErrorType.RangeError, RangeError],
|
|
22
|
+
['ReferenceError', ErrorType.ReferenceError, ReferenceError],
|
|
23
|
+
['SyntaxError', ErrorType.SyntaxError, SyntaxError],
|
|
24
|
+
['TypeError', ErrorType.TypeError, TypeError],
|
|
25
|
+
['RangeError', ErrorType.URIError, URIError],
|
|
26
|
+
])('throws %s when specified', (name, type, constructor) => {
|
|
27
|
+
expect(() => immediateError('test message', type)).toThrow(constructor)
|
|
28
|
+
})
|
|
29
|
+
// Custom Error Classes
|
|
30
|
+
test('throws a custom user-defined Error class', () => {
|
|
31
|
+
class MyCustomError extends Error {
|
|
32
|
+
constructor(message) {
|
|
33
|
+
super("Custom: " + message)
|
|
34
|
+
this.name = "MyCustomError"
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
expect(() => immediateError('Error!', MyCustomError)).toThrow(MyCustomError)
|
|
39
|
+
expect(() => immediateError('Error!', MyCustomError)).toThrow('Custom: Error!')
|
|
40
|
+
})
|
|
41
|
+
})
|
package/package.json
CHANGED
|
@@ -1,43 +1,73 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "immediate-error",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "7.0.0",
|
|
4
|
+
"description": "throw errors in fp",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "
|
|
7
|
+
"test": "jest index.test.js"
|
|
8
8
|
},
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
|
11
|
-
"url": "git+https://github.com/
|
|
11
|
+
"url": "git+https://github.com/in-fp/immediate-error.git"
|
|
12
12
|
},
|
|
13
13
|
"keywords": [
|
|
14
14
|
"error",
|
|
15
15
|
"throw",
|
|
16
16
|
"immediate-error",
|
|
17
|
-
"tool"
|
|
17
|
+
"tool",
|
|
18
|
+
"10x'ness",
|
|
19
|
+
"10x'ly made",
|
|
20
|
+
"10x",
|
|
21
|
+
"10x-engineer",
|
|
22
|
+
"10x engineer",
|
|
23
|
+
"10x engineering",
|
|
24
|
+
"jsfruit",
|
|
25
|
+
"libperson",
|
|
26
|
+
"libvegetable",
|
|
27
|
+
"bail",
|
|
28
|
+
"throw-error",
|
|
29
|
+
"sigma",
|
|
30
|
+
"lifesaver",
|
|
31
|
+
"framework",
|
|
32
|
+
"js",
|
|
33
|
+
"ecmascript",
|
|
34
|
+
"javascript",
|
|
35
|
+
"licensed",
|
|
36
|
+
"jest",
|
|
37
|
+
"67",
|
|
38
|
+
"fp",
|
|
39
|
+
"in-fp",
|
|
40
|
+
"sigmaskibidi",
|
|
41
|
+
"tj-commits",
|
|
42
|
+
"skibidi-toilet-hacker",
|
|
43
|
+
"stevelib"
|
|
18
44
|
],
|
|
19
|
-
"author": "
|
|
45
|
+
"author": "10x'ly Made",
|
|
20
46
|
"license": "Unlicense",
|
|
21
47
|
"bugs": {
|
|
22
|
-
"url": "https://github.com/
|
|
48
|
+
"url": "https://github.com/in-fp/immediate-error/issues"
|
|
23
49
|
},
|
|
24
|
-
"homepage": "https://github.com/
|
|
50
|
+
"homepage": "https://github.com/in-fp/immediate-error#readme",
|
|
25
51
|
"dependencies": {
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
52
|
+
"@positive-numbers/five": "^3.0.0",
|
|
53
|
+
"@positive-numbers/four": "^3.0.0",
|
|
54
|
+
"@positive-numbers/one": "^3.0.0",
|
|
55
|
+
"@positive-numbers/six": "^3.0.0",
|
|
56
|
+
"@positive-numbers/three": "^3.0.0",
|
|
57
|
+
"@positive-numbers/two": "^3.0.0",
|
|
58
|
+
"@positive-numbers/zero": "^3.0.0",
|
|
59
|
+
"attempt-statement": "^1.2.0",
|
|
60
|
+
"bail": "^1.0.5",
|
|
61
|
+
"construct-new": "^2.0.3",
|
|
62
|
+
"es-error-intrinsics": "^1.0.1",
|
|
63
|
+
"es-object-atoms": "^1.1.1",
|
|
64
|
+
"get-intrinsic": "^1.3.1",
|
|
35
65
|
"number-zero": "^1.0.3",
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"the-number-one": "^1.0.1",
|
|
39
|
-
"two": "^1.0.7",
|
|
40
|
-
"vanilla-javascript": "^1.1.1",
|
|
66
|
+
"switch-in-fp": "^3.0.0",
|
|
67
|
+
"true-value": "^2.0.5",
|
|
41
68
|
"yanoop": "^1.0.0"
|
|
69
|
+
},
|
|
70
|
+
"devDependencies": {
|
|
71
|
+
"jest": "^30.2.0"
|
|
42
72
|
}
|
|
43
73
|
}
|