immediate-error 6.2.0 → 6.4.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 -16
- package/index.d.ts +8 -1
- package/index.js +83 -114
- package/index.test.js +46 -0
- package/package.json +75 -46
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
|
|
|
@@ -35,17 +42,5 @@ class MyCustomError extends Error {
|
|
|
35
42
|
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
43
|
|
|
37
44
|
```
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
This module is great because it uses the best practices in JavaScript such as using dependencies like [`throw-error`](https://npmjs.com/package/throw-error) and [`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
|
-
## Show your support
|
|
44
|
-
|
|
45
|
-
[Follow me on GitHub](https://github.com/tj-commits) and star my repositories.
|
|
46
|
-
|
|
47
|
-
## is this a joke?
|
|
48
|
-
|
|
49
|
-
Of course not!
|
|
50
|
-
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
|
|
51
|
-
(Yes, it is.)
|
|
45
|
+
## License
|
|
46
|
+
Unlicense
|
package/index.d.ts
CHANGED
|
@@ -9,4 +9,11 @@ export enum ErrorType {
|
|
|
9
9
|
NativeAssertionError = 7
|
|
10
10
|
}
|
|
11
11
|
|
|
12
|
-
export
|
|
12
|
+
export type CustomError = {
|
|
13
|
+
new (message: string): never
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function immediateError(
|
|
17
|
+
message: string,
|
|
18
|
+
errorType?: ErrorType | CustomError
|
|
19
|
+
): never
|
package/index.js
CHANGED
|
@@ -1,120 +1,89 @@
|
|
|
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
|
-
const
|
|
18
|
-
|
|
19
|
-
const
|
|
20
|
-
const
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
const $
|
|
24
|
-
const $
|
|
25
|
-
const $
|
|
26
|
-
const $RangeError = require('es-errors/range')
|
|
27
|
-
const $ReferenceError = require('es-errors/ref')
|
|
28
|
-
const $SyntaxError = require('es-errors/syntax')
|
|
29
|
-
const $TypeError = require('es-errors/type')
|
|
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 seven = require("@positive-numbers/seven")
|
|
11
|
+
const { throwop, noop } = require("yanoop")
|
|
12
|
+
const { Switch } = require("switch-in-fp")
|
|
13
|
+
const assert = require("assert-fn")
|
|
14
|
+
const nativeAssert = require("node:assert")
|
|
15
|
+
const vm = require("node:vm")
|
|
16
|
+
const construct = require("construct-new")
|
|
17
|
+
const attempt = require("attempt-statement")
|
|
18
|
+
|
|
19
|
+
const $BaseError = require("es-errors")
|
|
20
|
+
const $AssertionError = assert.AssertionError
|
|
21
|
+
const $AggregateError = GetIntrinsic("%AggregateError%")
|
|
22
|
+
const $RangeError = require("es-errors/range")
|
|
23
|
+
const $ReferenceError = require("es-errors/ref")
|
|
24
|
+
const $SyntaxError = require("es-errors/syntax")
|
|
25
|
+
const $TypeError = require("es-errors/type")
|
|
30
26
|
const $NativeAssertionError = nativeAssert.AssertionError
|
|
31
27
|
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
28
|
+
const default_error = "ERROR!"
|
|
29
|
+
|
|
30
|
+
const ErrorType = $Object.freeze({
|
|
31
|
+
BaseError: zero,
|
|
32
|
+
AssertionError: one,
|
|
33
|
+
AggregateError: two,
|
|
34
|
+
RangeError: three,
|
|
35
|
+
ReferenceError: four,
|
|
36
|
+
SyntaxError: five,
|
|
37
|
+
TypeError: six,
|
|
38
|
+
NativeAssertionError: seven
|
|
43
39
|
})
|
|
44
40
|
|
|
45
|
-
exports.immediateError = function immediateError(message =
|
|
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
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
try {
|
|
92
|
-
error = new errorType(message)
|
|
93
|
-
} catch(err) {
|
|
94
|
-
[err] // put the error behind bars, where it belongs
|
|
95
|
-
error = new $BaseError(message)
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
const context = {
|
|
102
|
-
error,
|
|
103
|
-
throwError,
|
|
104
|
-
throwop,
|
|
105
|
-
rand: MathRandom(),
|
|
106
|
-
If
|
|
107
|
-
}
|
|
108
|
-
vm.createContext(context)
|
|
109
|
-
|
|
110
|
-
const script = new vm.Script(`
|
|
111
|
-
If(rand < 0.5).Then(() => {
|
|
112
|
-
throwError(error)
|
|
113
|
-
}).Else(() => {
|
|
114
|
-
throwop(error)
|
|
115
|
-
})`, { filename: `ERROR!`})
|
|
116
|
-
|
|
117
|
-
script.runInContext(context)
|
|
41
|
+
exports.immediateError = function immediateError(message = default_error, errorType = ErrorType.BaseError) {
|
|
42
|
+
var error
|
|
43
|
+
|
|
44
|
+
Switch(errorType)
|
|
45
|
+
.case(ErrorType.BaseError, function () {
|
|
46
|
+
error = construct({ target: $BaseError, args: [message] })
|
|
47
|
+
})
|
|
48
|
+
.case(ErrorType.AssertionError, function () {
|
|
49
|
+
error = construct({ target: $AssertionError, args: [message] })
|
|
50
|
+
})
|
|
51
|
+
.case(ErrorType.AggregateError, function () {
|
|
52
|
+
error = construct({ target: $AggregateError, args: [message] })
|
|
53
|
+
})
|
|
54
|
+
.case(ErrorType.RangeError, function () {
|
|
55
|
+
error = construct({ target: $RangeError, args: [message] })
|
|
56
|
+
})
|
|
57
|
+
.case(ErrorType.ReferenceError, function () {
|
|
58
|
+
error = construct({ target: $ReferenceError, args: [message] })
|
|
59
|
+
})
|
|
60
|
+
.case(ErrorType.SyntaxError, function () {
|
|
61
|
+
error = construct({ target: $SyntaxError, args: [message] })
|
|
62
|
+
})
|
|
63
|
+
.case(ErrorType.TypeError, function () {
|
|
64
|
+
error = construct({ target: $TypeError, args: [message] })
|
|
65
|
+
})
|
|
66
|
+
.case(ErrorType.NativeAssertionError, function () {
|
|
67
|
+
error = construct({ target: $NativeAssertionError, args: [message] })
|
|
68
|
+
})
|
|
69
|
+
.else(function () {
|
|
70
|
+
attempt(function() {
|
|
71
|
+
error = construct({ target: errorType, args: [message] })
|
|
72
|
+
}).rescue(function() {
|
|
73
|
+
error = construct({ target: $BaseError, args: [message] })
|
|
74
|
+
}).else(noop).ensure(noop).end()
|
|
75
|
+
})
|
|
76
|
+
.execute()
|
|
77
|
+
|
|
78
|
+
const context = {
|
|
79
|
+
error,
|
|
80
|
+
throwop
|
|
81
|
+
}
|
|
82
|
+
vm.createContext(context)
|
|
83
|
+
|
|
84
|
+
const script = construct({ target: vm.Script, args: [`throwop(error)`, { filename: default_error }] })
|
|
85
|
+
|
|
86
|
+
script.runInContext(context)
|
|
118
87
|
}
|
|
119
88
|
|
|
120
|
-
exports.ErrorType =
|
|
89
|
+
exports.ErrorType = ErrorType
|
package/index.test.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
const { immediateError, ErrorType } = require('immediate-error')
|
|
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
|
+
['RangeError', ErrorType.RangeError, RangeError],
|
|
20
|
+
['ReferenceError', ErrorType.ReferenceError, ReferenceError],
|
|
21
|
+
['SyntaxError', ErrorType.SyntaxError, SyntaxError],
|
|
22
|
+
['TypeError', ErrorType.TypeError, TypeError],
|
|
23
|
+
['AggregateError', ErrorType.AggregateError, AggregateError],
|
|
24
|
+
])('throws %s when specified', (name, type, constructor) => {
|
|
25
|
+
expect(() => immediateError('test message', type)).toThrow(constructor)
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
// Assertion Errors
|
|
29
|
+
test('throws NativeAssertionError (node:assert)', () => {
|
|
30
|
+
expect(() => immediateError('failed', ErrorType.NativeAssertionError))
|
|
31
|
+
.toThrow(assert.AssertionError)
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
// Custom Error Classes
|
|
35
|
+
test('throws a custom user-defined Error class', () => {
|
|
36
|
+
class MyCustomError extends Error {
|
|
37
|
+
constructor(message) {
|
|
38
|
+
super("Custom: " + message)
|
|
39
|
+
this.name = "MyCustomError"
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
expect(() => immediateError('Error!', MyCustomError)).toThrow(MyCustomError)
|
|
44
|
+
expect(() => immediateError('Error!', MyCustomError)).toThrow('Custom: Error!')
|
|
45
|
+
})
|
|
46
|
+
})
|
package/package.json
CHANGED
|
@@ -1,46 +1,75 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "immediate-error",
|
|
3
|
-
"version": "6.
|
|
4
|
-
"description": "
|
|
5
|
-
"main": "index.js",
|
|
6
|
-
"scripts": {
|
|
7
|
-
"test": "
|
|
8
|
-
},
|
|
9
|
-
"repository": {
|
|
10
|
-
"type": "git",
|
|
11
|
-
"url": "git+https://github.com/
|
|
12
|
-
},
|
|
13
|
-
"keywords": [
|
|
14
|
-
"error",
|
|
15
|
-
"throw",
|
|
16
|
-
"immediate-error",
|
|
17
|
-
"tool"
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
"
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
"
|
|
30
|
-
"
|
|
31
|
-
"
|
|
32
|
-
"
|
|
33
|
-
"
|
|
34
|
-
"
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"
|
|
45
|
-
|
|
46
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "immediate-error",
|
|
3
|
+
"version": "6.4.0",
|
|
4
|
+
"description": "throw errors in fp",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"test": "jest index.test.js"
|
|
8
|
+
},
|
|
9
|
+
"repository": {
|
|
10
|
+
"type": "git",
|
|
11
|
+
"url": "git+https://github.com/in-fp/immediate-error.git"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"error",
|
|
15
|
+
"throw",
|
|
16
|
+
"immediate-error",
|
|
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
|
+
"fox",
|
|
30
|
+
"sigma",
|
|
31
|
+
"lifesaver",
|
|
32
|
+
"framework",
|
|
33
|
+
"js",
|
|
34
|
+
"ecmascript",
|
|
35
|
+
"javascript",
|
|
36
|
+
"licensed",
|
|
37
|
+
"unlicensed-licensed-license",
|
|
38
|
+
"jest",
|
|
39
|
+
"67",
|
|
40
|
+
"fp",
|
|
41
|
+
"in-fp",
|
|
42
|
+
"sigmaskibidi",
|
|
43
|
+
"tj-commits",
|
|
44
|
+
"skibidi-toilet-hacker",
|
|
45
|
+
"stevelib"
|
|
46
|
+
],
|
|
47
|
+
"author": "10x'ly Made",
|
|
48
|
+
"license": "Unlicense",
|
|
49
|
+
"bugs": {
|
|
50
|
+
"url": "https://github.com/in-fp/immediate-error/issues"
|
|
51
|
+
},
|
|
52
|
+
"homepage": "https://github.com/in-fp/immediate-error#readme",
|
|
53
|
+
"dependencies": {
|
|
54
|
+
"@positive-numbers/five": "^3.0.0",
|
|
55
|
+
"@positive-numbers/four": "^3.0.0",
|
|
56
|
+
"@positive-numbers/one": "^3.0.0",
|
|
57
|
+
"@positive-numbers/seven": "^3.0.0",
|
|
58
|
+
"@positive-numbers/six": "^3.0.0",
|
|
59
|
+
"@positive-numbers/three": "^3.0.0",
|
|
60
|
+
"@positive-numbers/two": "^3.0.0",
|
|
61
|
+
"@positive-numbers/zero": "^3.0.0",
|
|
62
|
+
"assert-fn": "^1.0.1",
|
|
63
|
+
"attempt-statement": "^1.2.0",
|
|
64
|
+
"construct-new": "^2.0.3",
|
|
65
|
+
"es-errors": "^1.3.0",
|
|
66
|
+
"es-object-atoms": "^1.1.1",
|
|
67
|
+
"get-intrinsic": "^1.3.1",
|
|
68
|
+
"number-zero": "^1.0.3",
|
|
69
|
+
"switch-in-fp": "^3.0.0",
|
|
70
|
+
"yanoop": "^1.0.0"
|
|
71
|
+
},
|
|
72
|
+
"devDependencies": {
|
|
73
|
+
"jest": "^30.2.0"
|
|
74
|
+
}
|
|
75
|
+
}
|