immediate-error 9.0.1 → 11.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 +42 -2
- package/index.d.ts +53 -8
- package/index.js +78 -15
- package/index.test.js +159 -19
- package/package.json +22 -6
- package/CHANGELOG.md +0 -257
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# `immediate-error`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
General error-related utility library. 10x Engineered. It's for: throwing errors, getting cached error intrinsics, and try-cach alternative functional handling.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
```bash
|
|
@@ -10,7 +10,7 @@ $ npm install immediate-error
|
|
|
10
10
|
## Usage
|
|
11
11
|
Throwing Errors:
|
|
12
12
|
```javascript
|
|
13
|
-
const { immediateError, ErrorType, getError } = require("immediate-error")
|
|
13
|
+
const { immediateError, ErrorType, getError, throwWhatever } = require("immediate-error")
|
|
14
14
|
|
|
15
15
|
immediateError() // this will throw a regular Error with the default message of "ERROR!"
|
|
16
16
|
|
|
@@ -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)
|
|
@@ -38,6 +46,9 @@ class MyCustomError extends Error {
|
|
|
38
46
|
|
|
39
47
|
immediateError("Error!", MyCustomError) // throws a MyCustomError
|
|
40
48
|
|
|
49
|
+
throwWhatever("hi") // outputs: "Uncaught 'hi'"
|
|
50
|
+
throwWhatever() // outputs: "Uncaught undefined"
|
|
51
|
+
|
|
41
52
|
```
|
|
42
53
|
|
|
43
54
|
Getting Cached Errors:
|
|
@@ -47,6 +58,35 @@ const { getError, ErrorType } = require("immediate-error")
|
|
|
47
58
|
const TypeErrorConstructor = getError(ErrorType.TypeError)
|
|
48
59
|
|
|
49
60
|
console.log(TypeErrorConstructor === TypeError) // true
|
|
61
|
+
|
|
62
|
+
const VegetablesCannotTalkError = getError(ErrorType.VegetablesCannotTalkError)
|
|
63
|
+
|
|
64
|
+
try {
|
|
65
|
+
const Vegetable = require("libvegetable")
|
|
66
|
+
const vegetable = new Vegetable()
|
|
67
|
+
|
|
68
|
+
vegetable.greet()
|
|
69
|
+
} catch (error) {
|
|
70
|
+
console.log(error.constructor === VegetablesCannotTalkError) // true
|
|
71
|
+
}
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Error Handling (`attempt`)
|
|
75
|
+
|
|
76
|
+
```js
|
|
77
|
+
const { attempt } = require("immediate-error")
|
|
78
|
+
|
|
79
|
+
attempt(() => {
|
|
80
|
+
esuvnyuu.addisuaidof // thing that throws error
|
|
81
|
+
}).rescue(error => {
|
|
82
|
+
console.log(error.message) // outputs: esuvnyuu is not defined
|
|
83
|
+
}).else(() => {
|
|
84
|
+
// This will never happen
|
|
85
|
+
console.log("How did we get here?") // does not output anything, as this secition never runs.
|
|
86
|
+
}).ensure(() => {
|
|
87
|
+
// This will always happen
|
|
88
|
+
console.log(1) // outputs: 1
|
|
89
|
+
}).end() // you must end the statement with .end() or it wont run
|
|
50
90
|
```
|
|
51
91
|
|
|
52
92
|
### Changelog
|
package/index.d.ts
CHANGED
|
@@ -5,26 +5,71 @@ 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
|
|
21
|
-
export function getError(
|
|
21
|
+
export function getError(
|
|
22
|
+
errorType: ErrorType.ReferenceError
|
|
23
|
+
): typeof ReferenceError
|
|
22
24
|
export function getError(errorType: ErrorType.SyntaxError): typeof SyntaxError
|
|
23
25
|
export function getError(errorType: ErrorType.TypeError): typeof TypeError
|
|
24
26
|
export function getError(errorType: ErrorType.URIError): typeof URIError
|
|
25
|
-
export function getError(
|
|
27
|
+
export function getError(
|
|
28
|
+
errorType: ErrorType.FruitConsumptionError
|
|
29
|
+
): CustomError
|
|
30
|
+
export function getError(
|
|
31
|
+
errorType: ErrorType.VegetablesCannotTalkError
|
|
32
|
+
): CustomError
|
|
33
|
+
export function getError(errorType: ErrorType.PersonNotHungryError): CustomError
|
|
34
|
+
export function getError(errorType: ErrorType | CustomError): CustomError
|
|
26
35
|
|
|
27
36
|
export function immediateError(
|
|
28
|
-
message
|
|
37
|
+
message?: string,
|
|
29
38
|
errorType?: ErrorType | CustomError
|
|
30
|
-
): never
|
|
39
|
+
): never
|
|
40
|
+
|
|
41
|
+
export function throwWhatever(whateverToThrow: any): never
|
|
42
|
+
|
|
43
|
+
interface AttemptConstructor {
|
|
44
|
+
new (handler?: () => void): Attempt
|
|
45
|
+
|
|
46
|
+
(handler?: () => void): Attempt
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
declare class Attempt {
|
|
50
|
+
constructor(handler?: () => void)
|
|
51
|
+
|
|
52
|
+
trycode?: () => void
|
|
53
|
+
rescuehandler?: (error: any) => void
|
|
54
|
+
elsehandler?: () => void
|
|
55
|
+
ensurehandler?: (error: any) => void
|
|
56
|
+
|
|
57
|
+
rescue(handler: (error: any) => void): this
|
|
58
|
+
else(handler: () => void): this
|
|
59
|
+
ensure(handler: () => void): this
|
|
60
|
+
end(): void
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export const attempt: AttemptConstructor
|
|
64
|
+
|
|
65
|
+
export function delayedImmediateError(
|
|
66
|
+
message?: string,
|
|
67
|
+
errorType?: ErrorType | CustomError,
|
|
68
|
+
delay?: number
|
|
69
|
+
): Promise<never>
|
|
70
|
+
|
|
71
|
+
export function delayedImmediateError(
|
|
72
|
+
message: undefined,
|
|
73
|
+
errorType: undefined,
|
|
74
|
+
delay: number
|
|
75
|
+
): Promise<never>
|
package/index.js
CHANGED
|
@@ -2,15 +2,27 @@
|
|
|
2
2
|
|
|
3
3
|
const GetIntrinsic = require("es-intrinsic-cache")
|
|
4
4
|
const SimpleCache = require("simple-lru-cache")
|
|
5
|
+
const trust = require("@npm/mystery-function") // it's by npm, gotta trust
|
|
6
|
+
const Fruit = require("jsfruit")
|
|
7
|
+
const Vegetable = require("libvegetable")
|
|
8
|
+
const Person = require("libperson")
|
|
5
9
|
const isdash = require("is-")
|
|
6
10
|
const noop = require("n0p3-es2015-cjs")
|
|
7
11
|
const bail = require("bail")
|
|
8
|
-
const vm = require("node:vm")
|
|
9
12
|
const construct = require("construct-new")
|
|
10
13
|
const attempt = require("attempt-statement")
|
|
11
14
|
const trueValue = require("true-value")
|
|
12
15
|
const asArray = require("as-array")
|
|
13
|
-
const
|
|
16
|
+
const repeating = require("repeating")
|
|
17
|
+
const deepFreeze = require("deep-freeze-node3") // 3rd iteration of deep-freeze-node, and the only 10x one.
|
|
18
|
+
const concat = require("@rightpad/concat")
|
|
19
|
+
const NEWLINE = require("fizzbuzz-enterprise/source/main/constants/strings/delimiters/Newline") // hax
|
|
20
|
+
const falseValue = require("false-value")
|
|
21
|
+
const sleep = require("delay")
|
|
22
|
+
const call = require("node-call.then")
|
|
23
|
+
const bind = require("uncurry-x")(require("function-bind"))
|
|
24
|
+
const just = require("basic-functions")
|
|
25
|
+
const Null = require("qc-core").nullFn
|
|
14
26
|
|
|
15
27
|
const zero = require("@positive-numbers/zero")
|
|
16
28
|
const one = require("@positive-numbers/one")
|
|
@@ -19,14 +31,16 @@ const three = require("@positive-numbers/three")
|
|
|
19
31
|
const four = require("@positive-numbers/four")
|
|
20
32
|
const five = require("@positive-numbers/five")
|
|
21
33
|
const six = require("@positive-numbers/six")
|
|
22
|
-
const
|
|
34
|
+
const seven = require("@positive-numbers/seven")
|
|
35
|
+
const eight = require("@positive-numbers/eight")
|
|
36
|
+
const nine = require("@positive-numbers/nine")
|
|
37
|
+
const eleven = require("@positive-numbers/eleven")
|
|
38
|
+
const oneHundred = require("fizzbuzz-enterprise/source/main/constants/magic-numbers/Hundred")
|
|
23
39
|
|
|
24
40
|
const E = require("@uppercase-letters/e")
|
|
25
41
|
const O = require("@uppercase-letters/o")
|
|
26
42
|
const R = require("@uppercase-letters/r")
|
|
27
43
|
|
|
28
|
-
const concat = require("@rightpad/concat")
|
|
29
|
-
|
|
30
44
|
const $BaseError = require("es-error-intrinsics/Error")
|
|
31
45
|
const $EvalError = require("es-error-intrinsics/EvalError")
|
|
32
46
|
const $RangeError = require("es-error-intrinsics/RangeError")
|
|
@@ -47,6 +61,10 @@ const ErrorType = deepFreeze({
|
|
|
47
61
|
SyntaxError: four,
|
|
48
62
|
TypeError: five,
|
|
49
63
|
URIError: six,
|
|
64
|
+
|
|
65
|
+
FruitConsumptionError: seven,
|
|
66
|
+
VegetablesCannotTalkError: eight,
|
|
67
|
+
PersonNotHungryError: nine
|
|
50
68
|
})
|
|
51
69
|
|
|
52
70
|
const ErrorMap = construct({
|
|
@@ -62,8 +80,43 @@ const ErrorMap = construct({
|
|
|
62
80
|
ErrorMap.set(ErrorType.SyntaxError, $SyntaxError)
|
|
63
81
|
ErrorMap.set(ErrorType.TypeError, $TypeError)
|
|
64
82
|
ErrorMap.set(ErrorType.URIError, $URIError)
|
|
83
|
+
|
|
84
|
+
ErrorMap.set(ErrorType.FruitConsumptionError, (function() {
|
|
85
|
+
const fruit = construct({ target: Fruit })
|
|
86
|
+
try {
|
|
87
|
+
fruit
|
|
88
|
+
|
|
89
|
+
trust(repeating(concat("fruit.eat()", NEWLINE), eleven))
|
|
90
|
+
} catch (error) {
|
|
91
|
+
return error.constructor
|
|
92
|
+
}
|
|
93
|
+
})())
|
|
94
|
+
|
|
95
|
+
ErrorMap.set(ErrorType.VegetablesCannotTalkError, (function() {
|
|
96
|
+
const vegetable = construct({ target: Vegetable })
|
|
97
|
+
|
|
98
|
+
try {
|
|
99
|
+
vegetable.greet()
|
|
100
|
+
} catch (error) {
|
|
101
|
+
return error.constructor
|
|
102
|
+
}
|
|
103
|
+
})())
|
|
104
|
+
|
|
105
|
+
ErrorMap.set(ErrorType.PersonNotHungryError, (function() {
|
|
106
|
+
const person = construct({ target: Person })
|
|
107
|
+
person.hungry = falseValue()
|
|
108
|
+
try {
|
|
109
|
+
person.feed()
|
|
110
|
+
} catch (error) {
|
|
111
|
+
return error.constructor
|
|
112
|
+
}
|
|
113
|
+
})())
|
|
65
114
|
})()
|
|
66
115
|
|
|
116
|
+
function CreateSleepFunction(delay) {
|
|
117
|
+
return bind(sleep, Null(), delay)
|
|
118
|
+
}
|
|
119
|
+
|
|
67
120
|
function CreateError(error, message) {
|
|
68
121
|
return construct({ target: error, args: asArray(message) })
|
|
69
122
|
}
|
|
@@ -95,23 +148,33 @@ exports.immediateError = function immediateError(
|
|
|
95
148
|
captureStackTrace(error, immediateError)
|
|
96
149
|
}
|
|
97
150
|
|
|
98
|
-
|
|
99
|
-
error: error,
|
|
100
|
-
bail: bail,
|
|
101
|
-
}
|
|
151
|
+
exports.throwWhatever(error)
|
|
102
152
|
|
|
103
|
-
|
|
153
|
+
require("is-not-integer")() // how did we get here?
|
|
154
|
+
}
|
|
104
155
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
156
|
+
exports.delayedImmediateError = function delayedImmediateError(
|
|
157
|
+
message = default_error,
|
|
158
|
+
errorType = ErrorType.BaseError,
|
|
159
|
+
delay
|
|
160
|
+
) {
|
|
161
|
+
return call.then(just.call(CreateSleepFunction(delay)), () => {
|
|
162
|
+
return exports.immediateError(message, errorType)
|
|
108
163
|
})
|
|
109
|
-
|
|
110
|
-
script.runInContext(context)
|
|
111
164
|
}
|
|
112
165
|
|
|
113
166
|
exports.getError = function getError(errorType) {
|
|
114
167
|
return ErrorMap.get(errorType)
|
|
115
168
|
}
|
|
116
169
|
|
|
170
|
+
exports.throwWhatever = function throwWhatever(whateverToThrow) {
|
|
171
|
+
if (whateverToThrow) {
|
|
172
|
+
bail(whateverToThrow)
|
|
173
|
+
} else {
|
|
174
|
+
throw whateverToThrow // throw
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
exports.attempt = attempt
|
|
179
|
+
|
|
117
180
|
exports.ErrorType = 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,134 @@ 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
|
+
}
|
|
62
|
+
})
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
const { attempt } = require("./index")
|
|
66
|
+
|
|
67
|
+
describe("attempt utility", () => {
|
|
68
|
+
// Pattern support
|
|
69
|
+
test("works as a standard function", () => {
|
|
70
|
+
let called = false
|
|
71
|
+
attempt(() => {
|
|
72
|
+
called = true
|
|
73
|
+
}).end()
|
|
74
|
+
expect(called).toBe(true)
|
|
75
|
+
})
|
|
76
|
+
|
|
77
|
+
test("works as a constructor returning an instance", () => {
|
|
78
|
+
const instance = new attempt(() => {})
|
|
79
|
+
expect(instance).toBeDefined()
|
|
80
|
+
expect(typeof instance.rescue).toBe("function")
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
// Logic Flow
|
|
84
|
+
test("triggers rescue when the handler fails", () => {
|
|
85
|
+
let errorCaught = false
|
|
86
|
+
attempt(() => {
|
|
87
|
+
throw new Error("fail")
|
|
88
|
+
})
|
|
89
|
+
.rescue((e) => {
|
|
90
|
+
errorCaught = true
|
|
91
|
+
expect(e.message).toBe("fail")
|
|
92
|
+
})
|
|
93
|
+
.end()
|
|
94
|
+
expect(errorCaught).toBe(true)
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
test("triggers else only when the handler succeeds", () => {
|
|
98
|
+
let elseCalled = false
|
|
99
|
+
attempt(() => {
|
|
100
|
+
return "success"
|
|
101
|
+
})
|
|
102
|
+
.else(() => {
|
|
103
|
+
elseCalled = true
|
|
104
|
+
})
|
|
105
|
+
.end()
|
|
106
|
+
expect(elseCalled).toBe(true)
|
|
107
|
+
})
|
|
108
|
+
|
|
109
|
+
test("triggers ensure regardless of success or failure", () => {
|
|
110
|
+
let counter = 0
|
|
111
|
+
|
|
112
|
+
// Success case
|
|
113
|
+
attempt(() => {})
|
|
114
|
+
.ensure(() => {
|
|
115
|
+
counter++
|
|
116
|
+
})
|
|
117
|
+
.end()
|
|
118
|
+
|
|
119
|
+
// Failure case
|
|
120
|
+
attempt(() => {
|
|
121
|
+
throw new Error()
|
|
122
|
+
})
|
|
123
|
+
.rescue(() => {})
|
|
124
|
+
.ensure(() => {
|
|
125
|
+
counter++
|
|
126
|
+
})
|
|
127
|
+
.end()
|
|
128
|
+
|
|
129
|
+
expect(counter).toBe(2)
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
// Method Chaining
|
|
133
|
+
test("returns this (the instance) from chaining methods", () => {
|
|
134
|
+
const a = attempt(() => {})
|
|
135
|
+
const b = a.rescue(() => {})
|
|
136
|
+
const c = b.else(() => {})
|
|
137
|
+
const d = c.ensure(() => {})
|
|
138
|
+
|
|
139
|
+
expect(a).toBe(d)
|
|
140
|
+
})
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
const { delayedImmediateError } = require("./index")
|
|
144
|
+
|
|
145
|
+
describe("delayedImmediateError utility", () => {
|
|
146
|
+
// We use a small delay for testing
|
|
147
|
+
const SHORT_DELAY = 10
|
|
148
|
+
|
|
149
|
+
test("throws the error after a specified delay", async () => {
|
|
150
|
+
const start = Date.now()
|
|
151
|
+
|
|
152
|
+
// Since delayedImmediateError throws inside a promise chain/timeout,
|
|
153
|
+
// we catch it to verify the timing and error type.
|
|
154
|
+
try {
|
|
155
|
+
await delayedImmediateError("Delayed fail", ErrorType.BaseError, SHORT_DELAY)
|
|
156
|
+
} catch (error) {
|
|
157
|
+
const duration = Date.now() - start
|
|
158
|
+
expect(duration).toBeGreaterThanOrEqual(SHORT_DELAY)
|
|
159
|
+
expect(error.message).toBe("Delayed fail")
|
|
160
|
+
}
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
test("uses default error message and type if only delay is provided", async () => {
|
|
164
|
+
try {
|
|
165
|
+
await delayedImmediateError(undefined, undefined, SHORT_DELAY)
|
|
166
|
+
} catch (error) {
|
|
167
|
+
expect(error.message).toBe("ERROR!")
|
|
168
|
+
expect(error).toBeInstanceOf(Error)
|
|
169
|
+
}
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
test("respects custom error types in delayed mode", async () => {
|
|
173
|
+
try {
|
|
174
|
+
await delayedImmediateError("Type fail", ErrorType.TypeError, SHORT_DELAY)
|
|
175
|
+
} catch (error) {
|
|
176
|
+
expect(error).toBeInstanceOf(TypeError)
|
|
177
|
+
expect(error.message).toBe("Type fail")
|
|
178
|
+
}
|
|
39
179
|
})
|
|
40
180
|
})
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "immediate-error",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "11.0.0",
|
|
4
|
+
"description": "enterprise errors",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "jest index.test.js"
|
|
7
|
+
"test": "jest ./index.test.js"
|
|
8
8
|
},
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
|
@@ -49,10 +49,14 @@
|
|
|
49
49
|
},
|
|
50
50
|
"homepage": "https://github.com/enterprise-npm-ai/immediate-error#readme",
|
|
51
51
|
"dependencies": {
|
|
52
|
+
"@npm/mystery-function": "npm:eval-intrinsic-ai@^1.0.0",
|
|
53
|
+
"@positive-numbers/eight": "^3.0.0",
|
|
54
|
+
"@positive-numbers/eleven": "^3.0.0",
|
|
52
55
|
"@positive-numbers/five": "^3.0.0",
|
|
53
56
|
"@positive-numbers/four": "^3.0.0",
|
|
57
|
+
"@positive-numbers/nine": "^3.0.0",
|
|
54
58
|
"@positive-numbers/one": "^3.0.0",
|
|
55
|
-
"@positive-numbers/
|
|
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",
|
|
@@ -64,15 +68,27 @@
|
|
|
64
68
|
"as-array": "^2.0.0",
|
|
65
69
|
"attempt-statement": "^1.2.1",
|
|
66
70
|
"bail": "^1.0.5",
|
|
71
|
+
"basic-functions": "^1.0.6",
|
|
67
72
|
"construct-new": "^2.0.3",
|
|
68
73
|
"deep-freeze-node3": "^1.1.0",
|
|
74
|
+
"delay": "^5.0.0",
|
|
69
75
|
"es-error-intrinsics": "^1.0.1",
|
|
70
76
|
"es-intrinsic-cache": "^1.0.1",
|
|
77
|
+
"false-value": "^2.0.6",
|
|
78
|
+
"fizzbuzz-enterprise": "^1.0.0",
|
|
79
|
+
"function-bind": "^1.1.2",
|
|
71
80
|
"is-": "^1.0.0",
|
|
81
|
+
"is-not-integer": "^1.0.2",
|
|
82
|
+
"jsfruit": "^1.1.0",
|
|
83
|
+
"libperson": "^1.0.0",
|
|
84
|
+
"libvegetable": "^1.0.0",
|
|
72
85
|
"n0p3-es2015-cjs": "^1.0.1",
|
|
73
|
-
"
|
|
86
|
+
"node-call.then": "^1.0.0",
|
|
87
|
+
"qc-core": "^0.0.0",
|
|
88
|
+
"repeating": "^2.0.1",
|
|
74
89
|
"simple-lru-cache": "^0.0.2",
|
|
75
|
-
"true-value": "^2.0.5"
|
|
90
|
+
"true-value": "^2.0.5",
|
|
91
|
+
"uncurry-x": "^1.0.1"
|
|
76
92
|
},
|
|
77
93
|
"devDependencies": {
|
|
78
94
|
"jest": "^30.2.0"
|
package/CHANGELOG.md
DELETED
|
@@ -1,257 +0,0 @@
|
|
|
1
|
-
# Changelog
|
|
2
|
-
|
|
3
|
-
All notable changes to this project will be documented in this file.
|
|
4
|
-
Noitce: ai generated this changelog, so it might not be completely accurate.
|
|
5
|
-
|
|
6
|
-
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
7
|
-
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
8
|
-
|
|
9
|
-
## [9.0.1]
|
|
10
|
-
### Changed
|
|
11
|
-
- This changelog
|
|
12
|
-
- package.json description
|
|
13
|
-
|
|
14
|
-
## [9.0.0]
|
|
15
|
-
### Added
|
|
16
|
-
- `getError` function to get robustly cached errors
|
|
17
|
-
|
|
18
|
-
### Changed
|
|
19
|
-
- Internal logic to get errors to be more robust
|
|
20
|
-
- Removed dependency problem
|
|
21
|
-
|
|
22
|
-
## [8.1.0]
|
|
23
|
-
### THIS VERSION IS DEPRECATED: Dependency problem
|
|
24
|
-
|
|
25
|
-
### Added
|
|
26
|
-
- This changelog
|
|
27
|
-
|
|
28
|
-
## [8.0.0]
|
|
29
|
-
### THIS VERSION IS DEPRECATED: Dependency problem
|
|
30
|
-
|
|
31
|
-
### Added
|
|
32
|
-
- "use struct" directive for struct mode
|
|
33
|
-
- Dependencies: `is-`, `n0p3-es2015-cjs`, `deep-freeze-node3`, `@uppercase-letters/e`, `@uppercase-letters/o`, `@uppercase-letters/r`, `@rightpad/concat`, `as-array`
|
|
34
|
-
- `CreateError` helper function
|
|
35
|
-
- Improved error construction using functional programming utilities
|
|
36
|
-
|
|
37
|
-
### Changed
|
|
38
|
-
- Default error message now constructed using `concat` with uppercase letters
|
|
39
|
-
- Updated dependencies versions
|
|
40
|
-
- Simplified test file
|
|
41
|
-
|
|
42
|
-
### Removed
|
|
43
|
-
- `es-object-atoms`, `true-value`, `yanoop` dependencies
|
|
44
|
-
|
|
45
|
-
## [7.2.0]
|
|
46
|
-
### THIS VERSION IS DEPRECATED: Dependency problem
|
|
47
|
-
|
|
48
|
-
### Changed
|
|
49
|
-
- Minor adjustment in vm.Script args formatting
|
|
50
|
-
|
|
51
|
-
### Updated
|
|
52
|
-
- `@positive-numbers/zero` to `^4.0.0`
|
|
53
|
-
|
|
54
|
-
## [7.1.0]
|
|
55
|
-
|
|
56
|
-
### Changed
|
|
57
|
-
- Replaced `get-intrinsic` with `es-intrinsic-cache`
|
|
58
|
-
|
|
59
|
-
## [7.0.0]
|
|
60
|
-
|
|
61
|
-
### Added
|
|
62
|
-
- Support for `EvalError` and `URIError`
|
|
63
|
-
- `es-error-intrinsics` for better error handling
|
|
64
|
-
- `bail` for error throwing
|
|
65
|
-
- Stack trace capture using `Error.captureStackTrace`
|
|
66
|
-
|
|
67
|
-
### Changed
|
|
68
|
-
- Error types: Removed `AggregateError`, `AssertionError`, `NativeAssertionError`; Added `EvalError`, `URIError`
|
|
69
|
-
- Updated API to use `bail` in vm context
|
|
70
|
-
- Repository URL changed to `in-fp`
|
|
71
|
-
- Author changed to "10x'ly Made"
|
|
72
|
-
|
|
73
|
-
### Removed
|
|
74
|
-
- `assert-fn`, `es-object-atoms`, `number-zero` dependencies
|
|
75
|
-
- Some keywords from package.json
|
|
76
|
-
|
|
77
|
-
### Updated
|
|
78
|
-
- Tests to reflect new error types
|
|
79
|
-
|
|
80
|
-
## [6.4.0]
|
|
81
|
-
|
|
82
|
-
### Added
|
|
83
|
-
- Functional programming refactor using `switch-in-fp`, `construct-new`, `attempt-statement`
|
|
84
|
-
- `@positive-numbers/*` dependencies for numbers
|
|
85
|
-
- Jest tests
|
|
86
|
-
- TypeScript definitions (`index.d.ts`)
|
|
87
|
-
|
|
88
|
-
### Changed
|
|
89
|
-
- Major code refactor to FP style
|
|
90
|
-
- Dependencies updated to scoped packages
|
|
91
|
-
- Description changed to "throw errors in fp"
|
|
92
|
-
- Test script added
|
|
93
|
-
- Keywords updated with FP and other terms
|
|
94
|
-
|
|
95
|
-
### Removed
|
|
96
|
-
- Old dependencies replaced with new ones
|
|
97
|
-
|
|
98
|
-
## [6.3.0]
|
|
99
|
-
|
|
100
|
-
### Changed
|
|
101
|
-
- Code formatting and indentation
|
|
102
|
-
- License changed to "Unlicense"
|
|
103
|
-
- Removed some dependencies: `if`, `noop10`, `se7en`, `the-number-one`, `two`, `vanilla-javascript`
|
|
104
|
-
|
|
105
|
-
### Removed
|
|
106
|
-
- "why?" section from README
|
|
107
|
-
|
|
108
|
-
## [6.2.0]
|
|
109
|
-
|
|
110
|
-
### Changed
|
|
111
|
-
- Simplified code structure
|
|
112
|
-
- Updated vm script logic
|
|
113
|
-
- Removed dependencies: `bail`, `esm-wallaby`, `picocolors`
|
|
114
|
-
|
|
115
|
-
### Added
|
|
116
|
-
- `has-self-equality`, `noop10` dependencies
|
|
117
|
-
|
|
118
|
-
## [6.1.0]
|
|
119
|
-
### THIS VERSION IS DEPRECATED: Versions 5.0.0 through 6.1.0 are deprecated due to an internal Node.js error caused by esm-wallaby. Please upgrade to a newer version.
|
|
120
|
-
|
|
121
|
-
### Added
|
|
122
|
-
- `AggregateError` support
|
|
123
|
-
- Updated README with `AggregateError` example
|
|
124
|
-
- Updated TypeScript definitions
|
|
125
|
-
|
|
126
|
-
## [6.0.0]
|
|
127
|
-
### THIS VERSION IS DEPRECATED: Versions 5.0.0 through 6.1.0 are deprecated due to an internal Node.js error caused by esm-wallaby. Please upgrade to a newer version.
|
|
128
|
-
|
|
129
|
-
### Added
|
|
130
|
-
- `AggregateError` to error types
|
|
131
|
-
- `get-intrinsic` for intrinsics
|
|
132
|
-
|
|
133
|
-
### Changed
|
|
134
|
-
- Error types mapping
|
|
135
|
-
- Removed some dependencies: `jquery*`, `successor`, `vanilla-javascript`
|
|
136
|
-
|
|
137
|
-
### Updated
|
|
138
|
-
- `es-errors` to `^1.3.0`
|
|
139
|
-
|
|
140
|
-
## [5.1.1]
|
|
141
|
-
### THIS VERSION IS DEPRECATED: Versions 5.0.0 through 6.1.0 are deprecated due to an internal Node.js error caused by esm-wallaby. Please upgrade to a newer version.
|
|
142
|
-
|
|
143
|
-
### Changed
|
|
144
|
-
- Replaced `cli-color` with `picocolors`
|
|
145
|
-
|
|
146
|
-
## [5.1.0]
|
|
147
|
-
### THIS VERSION IS DEPRECATED: Versions 5.0.0 through 6.1.0 are deprecated due to an internal Node.js error caused by esm-wallaby. Please upgrade to a newer version.
|
|
148
|
-
|
|
149
|
-
### Changed
|
|
150
|
-
- Description updated to "Throw errors, better."
|
|
151
|
-
|
|
152
|
-
## [5.0.0]
|
|
153
|
-
### THIS VERSION IS DEPRECATED: Versions 5.0.0 through 6.1.0 are deprecated due to an internal Node.js error caused by esm-wallaby. Please upgrade to a newer version.
|
|
154
|
-
|
|
155
|
-
### Added
|
|
156
|
-
- `esm-wallaby` for ESM requiring
|
|
157
|
-
- `bail` for error throwing
|
|
158
|
-
|
|
159
|
-
### Changed
|
|
160
|
-
- Updated vm script to use `bail` with random logic
|
|
161
|
-
|
|
162
|
-
## [4.0.0]
|
|
163
|
-
|
|
164
|
-
### Added
|
|
165
|
-
- ES modules export style
|
|
166
|
-
- Support for custom error classes
|
|
167
|
-
- `es-errors` for proper error constructors
|
|
168
|
-
- TypeScript definitions
|
|
169
|
-
- More error types: `BaseError`, `AssertionError`, `AggregateError`, `NativeAssertionError`
|
|
170
|
-
|
|
171
|
-
### Changed
|
|
172
|
-
- Major API change: `immediateError(message, errorType)`
|
|
173
|
-
- Removed `fox` dependency
|
|
174
|
-
- Updated README with new usage
|
|
175
|
-
- License to "Unlicense"
|
|
176
|
-
|
|
177
|
-
### Removed
|
|
178
|
-
- Old dependencies
|
|
179
|
-
|
|
180
|
-
## [3.1.3]
|
|
181
|
-
|
|
182
|
-
### Added
|
|
183
|
-
- `FuckingError` class
|
|
184
|
-
- More number dependencies for error type values
|
|
185
|
-
- Updated README with `FuckingError` example
|
|
186
|
-
|
|
187
|
-
## [3.1.2]
|
|
188
|
-
|
|
189
|
-
### Changed
|
|
190
|
-
- Removed console.log from vm script
|
|
191
|
-
|
|
192
|
-
## [3.1.1]
|
|
193
|
-
|
|
194
|
-
### Removed
|
|
195
|
-
- `integer-value-positive-zero` dependency
|
|
196
|
-
|
|
197
|
-
## [3.1.0]
|
|
198
|
-
|
|
199
|
-
### Added
|
|
200
|
-
- Many new dependencies for numbers, jQuery, etc.
|
|
201
|
-
- Complex vm context with random error throwing
|
|
202
|
-
- Updated README
|
|
203
|
-
|
|
204
|
-
### Changed
|
|
205
|
-
- Default message to aggressive one
|
|
206
|
-
- Error creation using packages for numbers
|
|
207
|
-
|
|
208
|
-
## [3.0.0]
|
|
209
|
-
|
|
210
|
-
### Added
|
|
211
|
-
- Repository, keywords, author info
|
|
212
|
-
- Many dependencies: `vanilla-javascript`, `vapor-js-npm`, etc.
|
|
213
|
-
- vm context for error throwing
|
|
214
|
-
|
|
215
|
-
### Changed
|
|
216
|
-
- Description to "Immediate-error"
|
|
217
|
-
- Major code changes with more complexity
|
|
218
|
-
|
|
219
|
-
## [2.1.0]
|
|
220
|
-
|
|
221
|
-
### Added
|
|
222
|
-
- ERROR object with multiple error types
|
|
223
|
-
- eval-based error throwing
|
|
224
|
-
|
|
225
|
-
### Changed
|
|
226
|
-
- License to "UNLICENSED"
|
|
227
|
-
- Removed dependencies
|
|
228
|
-
|
|
229
|
-
### Updated
|
|
230
|
-
- README with more examples
|
|
231
|
-
|
|
232
|
-
## [2.0.0]
|
|
233
|
-
|
|
234
|
-
### Changed
|
|
235
|
-
- Simplified to throw `new Error(message)` with default message (logic that makes sense)
|
|
236
|
-
- Updated README example
|
|
237
|
-
|
|
238
|
-
## [1.0.1]
|
|
239
|
-
|
|
240
|
-
### Changed
|
|
241
|
-
- Author to "tj-commits/87f"
|
|
242
|
-
|
|
243
|
-
## [1.0.0]
|
|
244
|
-
|
|
245
|
-
### Added
|
|
246
|
-
- README.md with usage
|
|
247
|
-
- Self-dependency (circular?)
|
|
248
|
-
|
|
249
|
-
### Changed
|
|
250
|
-
- Version to 1.0.0
|
|
251
|
-
|
|
252
|
-
## [0.0.1]
|
|
253
|
-
|
|
254
|
-
### Added
|
|
255
|
-
- Initial release
|
|
256
|
-
- Basic index.js that requires 'fox', which is a pure ESM module, so it would throw an error since the package is CommonJS
|
|
257
|
-
- package.json with basic info
|