immediate-error 9.0.1 → 10.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/CHANGELOG.md +4 -0
- package/README.md +19 -0
- package/index.d.ts +9 -6
- package/index.js +48 -15
- package/index.test.js +42 -19
- package/package.json +12 -1
package/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,10 @@ Noitce: ai generated this changelog, so it might not be completely accurate.
|
|
|
6
6
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
7
7
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
8
8
|
|
|
9
|
+
## [10.0.0]
|
|
10
|
+
### Added
|
|
11
|
+
- Domain specific errors: `FruitConsumptionError`, `VegetablesCannotTalkError`, and `PersonNotHungryError`
|
|
12
|
+
|
|
9
13
|
## [9.0.1]
|
|
10
14
|
### Changed
|
|
11
15
|
- This changelog
|
package/README.md
CHANGED
|
@@ -30,6 +30,14 @@ immediateError("Syntax error", ErrorType.SyntaxError) // throws a SyntaxError
|
|
|
30
30
|
|
|
31
31
|
immediateError("URI Error", ErrorType.URIError) // throws a URIError
|
|
32
32
|
|
|
33
|
+
// Domain-specific Enterprise Errors:
|
|
34
|
+
|
|
35
|
+
immediateError("fruit consumption error", ErrorType.FruitConsumptionError) // throws FruitConsumptionError
|
|
36
|
+
|
|
37
|
+
immediateError("vegetables cannot talk error", ErrorType.VegetablesCannotTalkError) // throws a VegetablesCannotTalkError
|
|
38
|
+
|
|
39
|
+
immediateError("person not hungry error", ErrorType.PersonNotHungryError) // throws a PersonNotHungryError
|
|
40
|
+
|
|
33
41
|
class MyCustomError extends Error {
|
|
34
42
|
constructor (message) {
|
|
35
43
|
super("Custom: " + message)
|
|
@@ -47,6 +55,17 @@ const { getError, ErrorType } = require("immediate-error")
|
|
|
47
55
|
const TypeErrorConstructor = getError(ErrorType.TypeError)
|
|
48
56
|
|
|
49
57
|
console.log(TypeErrorConstructor === TypeError) // true
|
|
58
|
+
|
|
59
|
+
const VegetablesCannotTalkError = getError(ErrorType.VegetablesCannotTalkError)
|
|
60
|
+
|
|
61
|
+
try {
|
|
62
|
+
const Vegetable = require("libvegetable")
|
|
63
|
+
const vegetable = new Vegetable()
|
|
64
|
+
|
|
65
|
+
vegetable.greet()
|
|
66
|
+
} catch (error) {
|
|
67
|
+
console.log(error.constructor === VegetablesCannotTalkError) // true
|
|
68
|
+
}
|
|
50
69
|
```
|
|
51
70
|
|
|
52
71
|
### Changelog
|
package/index.d.ts
CHANGED
|
@@ -5,16 +5,16 @@ export enum ErrorType {
|
|
|
5
5
|
ReferenceError = 3,
|
|
6
6
|
SyntaxError = 4,
|
|
7
7
|
TypeError = 5,
|
|
8
|
-
URIError = 6
|
|
8
|
+
URIError = 6,
|
|
9
|
+
FruitConsumptionError = 7,
|
|
10
|
+
VegetablesCannotTalkError = 8,
|
|
11
|
+
PersonNotHungryError = 9
|
|
9
12
|
}
|
|
10
13
|
|
|
11
14
|
export type CustomError = {
|
|
12
15
|
new (message: string): Error
|
|
13
16
|
}
|
|
14
17
|
|
|
15
|
-
/**
|
|
16
|
-
* Enterprise retrieval signatures for specific ErrorTypes
|
|
17
|
-
*/
|
|
18
18
|
export function getError(errorType: ErrorType.BaseError): typeof Error
|
|
19
19
|
export function getError(errorType: ErrorType.EvalError): typeof EvalError
|
|
20
20
|
export function getError(errorType: ErrorType.RangeError): typeof RangeError
|
|
@@ -22,9 +22,12 @@ export function getError(errorType: ErrorType.ReferenceError): typeof ReferenceE
|
|
|
22
22
|
export function getError(errorType: ErrorType.SyntaxError): typeof SyntaxError
|
|
23
23
|
export function getError(errorType: ErrorType.TypeError): typeof TypeError
|
|
24
24
|
export function getError(errorType: ErrorType.URIError): typeof URIError
|
|
25
|
-
export function getError(errorType: ErrorType): CustomError
|
|
25
|
+
export function getError(errorType: ErrorType.FruitConsumptionError): CustomError
|
|
26
|
+
export function getError(errorType: ErrorType.VegetablesCannotTalkError): CustomError
|
|
27
|
+
export function getError(errorType: ErrorType.PersonNotHungryError): CustomError
|
|
28
|
+
export function getError(errorType: ErrorType | CustomError): CustomError
|
|
26
29
|
|
|
27
30
|
export function immediateError(
|
|
28
|
-
message
|
|
31
|
+
message?: string,
|
|
29
32
|
errorType?: ErrorType | CustomError
|
|
30
33
|
): never
|
package/index.js
CHANGED
|
@@ -2,15 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
const GetIntrinsic = require("es-intrinsic-cache")
|
|
4
4
|
const SimpleCache = require("simple-lru-cache")
|
|
5
|
+
const Fruit = require("jsfruit")
|
|
6
|
+
const Vegetable = require("libvegetable")
|
|
7
|
+
const Person = require("libperson")
|
|
5
8
|
const isdash = require("is-")
|
|
6
9
|
const noop = require("n0p3-es2015-cjs")
|
|
7
10
|
const bail = require("bail")
|
|
8
|
-
const vm = require("node:vm")
|
|
9
11
|
const construct = require("construct-new")
|
|
10
12
|
const attempt = require("attempt-statement")
|
|
11
13
|
const trueValue = require("true-value")
|
|
12
14
|
const asArray = require("as-array")
|
|
15
|
+
const repeating = require("repeating")
|
|
13
16
|
const deepFreeze = require("deep-freeze-node3") // 3rd iteration of deep-freeze-node.
|
|
17
|
+
const concat = require("@rightpad/concat")
|
|
18
|
+
const NEWLINE = require("fizzbuzz-enterprise/source/main/constants/strings/delimiters/Newline")
|
|
19
|
+
const falseValue = require("false-value")
|
|
14
20
|
|
|
15
21
|
const zero = require("@positive-numbers/zero")
|
|
16
22
|
const one = require("@positive-numbers/one")
|
|
@@ -19,14 +25,16 @@ const three = require("@positive-numbers/three")
|
|
|
19
25
|
const four = require("@positive-numbers/four")
|
|
20
26
|
const five = require("@positive-numbers/five")
|
|
21
27
|
const six = require("@positive-numbers/six")
|
|
28
|
+
const seven = require("@positive-numbers/seven")
|
|
29
|
+
const eight = require("@positive-numbers/eight")
|
|
30
|
+
const nine = require("@positive-numbers/nine")
|
|
31
|
+
const eleven = require("@positive-numbers/eleven")
|
|
22
32
|
const oneHundred = require("@positive-numbers/one-hundred")
|
|
23
33
|
|
|
24
34
|
const E = require("@uppercase-letters/e")
|
|
25
35
|
const O = require("@uppercase-letters/o")
|
|
26
36
|
const R = require("@uppercase-letters/r")
|
|
27
37
|
|
|
28
|
-
const concat = require("@rightpad/concat")
|
|
29
|
-
|
|
30
38
|
const $BaseError = require("es-error-intrinsics/Error")
|
|
31
39
|
const $EvalError = require("es-error-intrinsics/EvalError")
|
|
32
40
|
const $RangeError = require("es-error-intrinsics/RangeError")
|
|
@@ -47,6 +55,10 @@ const ErrorType = deepFreeze({
|
|
|
47
55
|
SyntaxError: four,
|
|
48
56
|
TypeError: five,
|
|
49
57
|
URIError: six,
|
|
58
|
+
|
|
59
|
+
FruitConsumptionError: seven,
|
|
60
|
+
VegetablesCannotTalkError: eight,
|
|
61
|
+
PersonNotHungryError: nine
|
|
50
62
|
})
|
|
51
63
|
|
|
52
64
|
const ErrorMap = construct({
|
|
@@ -62,6 +74,37 @@ const ErrorMap = construct({
|
|
|
62
74
|
ErrorMap.set(ErrorType.SyntaxError, $SyntaxError)
|
|
63
75
|
ErrorMap.set(ErrorType.TypeError, $TypeError)
|
|
64
76
|
ErrorMap.set(ErrorType.URIError, $URIError)
|
|
77
|
+
|
|
78
|
+
ErrorMap.set(ErrorType.FruitConsumptionError, (function() {
|
|
79
|
+
const fruit = construct({ target: Fruit })
|
|
80
|
+
try {
|
|
81
|
+
fruit
|
|
82
|
+
|
|
83
|
+
eval(repeating(concat("fruit.eat()", NEWLINE), eleven))
|
|
84
|
+
} catch (error) {
|
|
85
|
+
return error.constructor
|
|
86
|
+
}
|
|
87
|
+
})())
|
|
88
|
+
|
|
89
|
+
ErrorMap.set(ErrorType.VegetablesCannotTalkError, (function() {
|
|
90
|
+
const vegetable = construct({ target: Vegetable })
|
|
91
|
+
|
|
92
|
+
try {
|
|
93
|
+
vegetable.greet()
|
|
94
|
+
} catch (error) {
|
|
95
|
+
return error.constructor
|
|
96
|
+
}
|
|
97
|
+
})())
|
|
98
|
+
|
|
99
|
+
ErrorMap.set(ErrorType.PersonNotHungryError, (function() {
|
|
100
|
+
const person = construct({ target: Person })
|
|
101
|
+
person.hungry = falseValue()
|
|
102
|
+
try {
|
|
103
|
+
person.feed()
|
|
104
|
+
} catch (error) {
|
|
105
|
+
return error.constructor
|
|
106
|
+
}
|
|
107
|
+
})())
|
|
65
108
|
})()
|
|
66
109
|
|
|
67
110
|
function CreateError(error, message) {
|
|
@@ -95,19 +138,9 @@ exports.immediateError = function immediateError(
|
|
|
95
138
|
captureStackTrace(error, immediateError)
|
|
96
139
|
}
|
|
97
140
|
|
|
98
|
-
|
|
99
|
-
error: error,
|
|
100
|
-
bail: bail,
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
vm.createContext(context)
|
|
104
|
-
|
|
105
|
-
const script = construct({
|
|
106
|
-
target: vm.Script,
|
|
107
|
-
args: ["bail(error)", { filename: default_error }],
|
|
108
|
-
})
|
|
141
|
+
bail(error)
|
|
109
142
|
|
|
110
|
-
|
|
143
|
+
require("is-not-integer")() // how did we get here?
|
|
111
144
|
}
|
|
112
145
|
|
|
113
146
|
exports.getError = function getError(errorType) {
|
package/index.test.js
CHANGED
|
@@ -1,32 +1,45 @@
|
|
|
1
|
-
const { immediateError, ErrorType } = require(
|
|
1
|
+
const { immediateError, ErrorType } = require("./index")
|
|
2
2
|
|
|
3
|
-
describe(
|
|
3
|
+
describe("immediateError utility", () => {
|
|
4
4
|
|
|
5
5
|
// Basic Usage
|
|
6
|
-
test(
|
|
6
|
+
test("throws a regular Error with default message when no arguments are passed", () => {
|
|
7
7
|
expect(() => immediateError()).toThrow(Error)
|
|
8
|
-
expect(() => immediateError()).toThrow(
|
|
8
|
+
expect(() => immediateError()).toThrow("ERROR!")
|
|
9
9
|
})
|
|
10
10
|
|
|
11
|
-
test(
|
|
12
|
-
expect(() => immediateError(
|
|
13
|
-
expect(() => immediateError(
|
|
11
|
+
test("throws a regular Error with a custom message", () => {
|
|
12
|
+
expect(() => immediateError("Aaaaah")).toThrow(Error)
|
|
13
|
+
expect(() => immediateError("Aaaaah")).toThrow("Aaaaah")
|
|
14
14
|
})
|
|
15
15
|
|
|
16
16
|
// Native Error Types
|
|
17
17
|
test.each([
|
|
18
|
-
[
|
|
19
|
-
[
|
|
20
|
-
[
|
|
21
|
-
[
|
|
22
|
-
[
|
|
23
|
-
[
|
|
24
|
-
[
|
|
25
|
-
])(
|
|
26
|
-
expect(() => immediateError(
|
|
18
|
+
["BaseError", ErrorType.BaseError, Error],
|
|
19
|
+
["EvalError", ErrorType.EvalError, EvalError],
|
|
20
|
+
["RangeError", ErrorType.RangeError, RangeError],
|
|
21
|
+
["ReferenceError", ErrorType.ReferenceError, ReferenceError],
|
|
22
|
+
["SyntaxError", ErrorType.SyntaxError, SyntaxError],
|
|
23
|
+
["TypeError", ErrorType.TypeError, TypeError],
|
|
24
|
+
["URIError", ErrorType.URIError, URIError],
|
|
25
|
+
])("throws %s when specified", (name, type, constructor) => {
|
|
26
|
+
expect(() => immediateError("test message", type)).toThrow(constructor)
|
|
27
27
|
})
|
|
28
|
+
|
|
29
|
+
// Enterprise Domain-Specific Error Types
|
|
30
|
+
test.each([
|
|
31
|
+
["FruitConsumptionError", ErrorType.FruitConsumptionError],
|
|
32
|
+
["VegetablesCannotTalkError", ErrorType.VegetablesCannotTalkError],
|
|
33
|
+
["PersonNotHungryError", ErrorType.PersonNotHungryError],
|
|
34
|
+
])("throws domain-specific %s correctly", (name, type) => {
|
|
35
|
+
// We check that it throws an instance of the constructor cached in ErrorMap
|
|
36
|
+
const expectedConstructor = require("./index").getError(type)
|
|
37
|
+
expect(() => immediateError("enterprise failure", type)).toThrow(expectedConstructor)
|
|
38
|
+
expect(() => immediateError("enterprise failure", type)).toThrow("enterprise failure")
|
|
39
|
+
})
|
|
40
|
+
|
|
28
41
|
// Custom Error Classes
|
|
29
|
-
test(
|
|
42
|
+
test("throws a custom user-defined Error class", () => {
|
|
30
43
|
class MyCustomError extends Error {
|
|
31
44
|
constructor(message) {
|
|
32
45
|
super("Custom: " + message)
|
|
@@ -34,7 +47,17 @@ describe('immediateError utility', () => {
|
|
|
34
47
|
}
|
|
35
48
|
}
|
|
36
49
|
|
|
37
|
-
expect(() => immediateError(
|
|
38
|
-
expect(() => immediateError(
|
|
50
|
+
expect(() => immediateError("Error!", MyCustomError)).toThrow(MyCustomError)
|
|
51
|
+
expect(() => immediateError("Error!", MyCustomError)).toThrow("Custom: Error!")
|
|
52
|
+
})
|
|
53
|
+
|
|
54
|
+
// Stack Trace Integrity
|
|
55
|
+
test("captures stack trace correctly and hides internal frames", () => {
|
|
56
|
+
try {
|
|
57
|
+
immediateError("stack check")
|
|
58
|
+
} catch (error) {
|
|
59
|
+
// The first line of the stack should be the caller, not immediateError itself
|
|
60
|
+
expect(error.stack).not.toMatch(/at immediateError/)
|
|
61
|
+
}
|
|
39
62
|
})
|
|
40
63
|
})
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "immediate-error",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "10.0.0",
|
|
4
4
|
"description": "throw errors",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -49,10 +49,14 @@
|
|
|
49
49
|
},
|
|
50
50
|
"homepage": "https://github.com/enterprise-npm-ai/immediate-error#readme",
|
|
51
51
|
"dependencies": {
|
|
52
|
+
"@positive-numbers/eight": "^3.0.0",
|
|
53
|
+
"@positive-numbers/eleven": "^3.0.0",
|
|
52
54
|
"@positive-numbers/five": "^3.0.0",
|
|
53
55
|
"@positive-numbers/four": "^3.0.0",
|
|
56
|
+
"@positive-numbers/nine": "^3.0.0",
|
|
54
57
|
"@positive-numbers/one": "^3.0.0",
|
|
55
58
|
"@positive-numbers/one-hundred": "^3.0.0",
|
|
59
|
+
"@positive-numbers/seven": "^3.0.0",
|
|
56
60
|
"@positive-numbers/six": "^3.0.0",
|
|
57
61
|
"@positive-numbers/three": "^3.0.0",
|
|
58
62
|
"@positive-numbers/two": "^3.0.0",
|
|
@@ -68,9 +72,16 @@
|
|
|
68
72
|
"deep-freeze-node3": "^1.1.0",
|
|
69
73
|
"es-error-intrinsics": "^1.0.1",
|
|
70
74
|
"es-intrinsic-cache": "^1.0.1",
|
|
75
|
+
"false-value": "^2.0.6",
|
|
76
|
+
"fizzbuzz-enterprise": "^1.0.0",
|
|
71
77
|
"is-": "^1.0.0",
|
|
78
|
+
"is-not-integer": "^1.0.2",
|
|
79
|
+
"jsfruit": "^1.1.0",
|
|
80
|
+
"libperson": "^1.0.0",
|
|
81
|
+
"libvegetable": "^1.0.0",
|
|
72
82
|
"n0p3-es2015-cjs": "^1.0.1",
|
|
73
83
|
"number-zero": "^1.0.3",
|
|
84
|
+
"repeating": "^2.0.1",
|
|
74
85
|
"simple-lru-cache": "^0.0.2",
|
|
75
86
|
"true-value": "^2.0.5"
|
|
76
87
|
},
|