immediate-error 12.2.1 → 12.2.3
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/.github/workflows/lint.yml +20 -0
- package/README.md +3 -0
- package/eslint.config.mjs +3 -0
- package/index.d.ts +5 -0
- package/index.js +197 -106
- package/index.test.js +266 -257
- package/package.json +144 -139
package/index.test.js
CHANGED
|
@@ -1,257 +1,266 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
expect(() => immediateError()).toThrow(
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
expect(() => immediateError("Aaaaah")).toThrow(
|
|
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
|
-
|
|
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
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
const
|
|
135
|
-
const
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
}
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
const
|
|
225
|
-
expect(
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
test
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
// tests
|
|
3
|
+
|
|
4
|
+
const {
|
|
5
|
+
immediateError,
|
|
6
|
+
ErrorType,
|
|
7
|
+
throwWhatever,
|
|
8
|
+
getError,
|
|
9
|
+
MESSAGES
|
|
10
|
+
} = require("./index")
|
|
11
|
+
|
|
12
|
+
describe("immediateError utility", () => {
|
|
13
|
+
test("throws a regular Error with default message when no arguments are passed", () => {
|
|
14
|
+
expect(() => immediateError()).toThrow(Error)
|
|
15
|
+
expect(() => immediateError()).toThrow("ERROR!")
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
test("throws a regular Error with a custom message", () => {
|
|
19
|
+
expect(() => immediateError("Aaaaah")).toThrow(Error)
|
|
20
|
+
expect(() => immediateError("Aaaaah")).toThrow("Aaaaah")
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
test.each([
|
|
24
|
+
["BaseError", ErrorType.BaseError, Error],
|
|
25
|
+
["EvalError", ErrorType.EvalError, EvalError],
|
|
26
|
+
["RangeError", ErrorType.RangeError, RangeError],
|
|
27
|
+
["ReferenceError", ErrorType.ReferenceError, ReferenceError],
|
|
28
|
+
["SyntaxError", ErrorType.SyntaxError, SyntaxError],
|
|
29
|
+
["TypeError", ErrorType.TypeError, TypeError],
|
|
30
|
+
["URIError", ErrorType.URIError, URIError],
|
|
31
|
+
])("throws % when specified", (name, type, constructor) => {
|
|
32
|
+
expect(() => immediateError("test message", type)).toThrow(constructor)
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
test.each([
|
|
36
|
+
["FruitConsumptionError", ErrorType.FruitConsumptionError],
|
|
37
|
+
["VegetablesDoNotTalkError", ErrorType.VegetablesDoNotTalkError],
|
|
38
|
+
["PersonNotHungryError", ErrorType.PersonNotHungryError],
|
|
39
|
+
["PortionsError", ErrorType.PortionsError],
|
|
40
|
+
["FalseJSValidationFailedToPassError", ErrorType.FalseJSValidationFailedToPassError]
|
|
41
|
+
])("throws domain-specific % correctly", (name, type) => {
|
|
42
|
+
const expectedConstructor = getError(type)
|
|
43
|
+
expect(() => immediateError("enterprise failure", type)).toThrow(expectedConstructor)
|
|
44
|
+
expect(() => immediateError("enterprise failure", type)).toThrow("enterprise failure")
|
|
45
|
+
})
|
|
46
|
+
|
|
47
|
+
test("throws a custom user-defined Error class", () => {
|
|
48
|
+
class MyCustomError extends Error {
|
|
49
|
+
constructor(message) {
|
|
50
|
+
super("Custom: " + message)
|
|
51
|
+
this.name = "MyCustomError"
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
expect(() => immediateError("Error!", MyCustomError)).toThrow(MyCustomError)
|
|
56
|
+
expect(() => immediateError("Error!", MyCustomError)).toThrow(
|
|
57
|
+
"Custom: Error!",
|
|
58
|
+
)
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
test("captures stack trace correctly and hides internal frames", () => {
|
|
62
|
+
try {
|
|
63
|
+
immediateError("stack check")
|
|
64
|
+
} catch (error) {
|
|
65
|
+
expect(error.stack).not.toMatch(/at immediateError/)
|
|
66
|
+
}
|
|
67
|
+
})
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
const { attempt } = require("./index")
|
|
71
|
+
|
|
72
|
+
describe("attempt utility", () => {
|
|
73
|
+
test("works as a standard function", () => {
|
|
74
|
+
let called = false
|
|
75
|
+
attempt(() => {
|
|
76
|
+
called = true
|
|
77
|
+
}).end()
|
|
78
|
+
expect(called).toBe(true)
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
test("works as a constructor returning an instance", () => {
|
|
82
|
+
const instance = new attempt(() => {})
|
|
83
|
+
expect(instance).toBeDefined()
|
|
84
|
+
expect(typeof instance.rescue).toBe("function")
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
test("triggers rescue when the handler fails", () => {
|
|
88
|
+
let errorCaught = false
|
|
89
|
+
attempt(() => {
|
|
90
|
+
throw new Error("fail")
|
|
91
|
+
})
|
|
92
|
+
.rescue((e) => {
|
|
93
|
+
errorCaught = true
|
|
94
|
+
expect(e.message).toBe("fail")
|
|
95
|
+
})
|
|
96
|
+
.end()
|
|
97
|
+
expect(errorCaught).toBe(true)
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
test("triggers else only when the handler succeeds", () => {
|
|
101
|
+
let elseCalled = false
|
|
102
|
+
attempt(() => {
|
|
103
|
+
return "success"
|
|
104
|
+
})
|
|
105
|
+
.else(() => {
|
|
106
|
+
elseCalled = true
|
|
107
|
+
})
|
|
108
|
+
.end()
|
|
109
|
+
expect(elseCalled).toBe(true)
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
test("triggers ensure regardless of success or failure", () => {
|
|
113
|
+
let counter = 0
|
|
114
|
+
|
|
115
|
+
attempt(() => {})
|
|
116
|
+
.ensure(() => {
|
|
117
|
+
counter++
|
|
118
|
+
})
|
|
119
|
+
.end()
|
|
120
|
+
|
|
121
|
+
attempt(() => {
|
|
122
|
+
throw new Error()
|
|
123
|
+
})
|
|
124
|
+
.rescue(() => {})
|
|
125
|
+
.ensure(() => {
|
|
126
|
+
counter++
|
|
127
|
+
})
|
|
128
|
+
.end()
|
|
129
|
+
|
|
130
|
+
expect(counter).toBe(2)
|
|
131
|
+
})
|
|
132
|
+
|
|
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 { delayedError } = require("./index")
|
|
144
|
+
|
|
145
|
+
describe("delayedError utility", () => {
|
|
146
|
+
const SHORT_DELAY = 10
|
|
147
|
+
|
|
148
|
+
test("throws the error after a specified delay", async () => {
|
|
149
|
+
const start = Date.now()
|
|
150
|
+
|
|
151
|
+
try {
|
|
152
|
+
await delayedError("Delayed fail", ErrorType.BaseError, SHORT_DELAY)
|
|
153
|
+
} catch (error) {
|
|
154
|
+
const duration = Date.now() - start
|
|
155
|
+
expect(duration).toBeGreaterThanOrEqual(SHORT_DELAY)
|
|
156
|
+
expect(error.message).toBe("Delayed fail")
|
|
157
|
+
}
|
|
158
|
+
})
|
|
159
|
+
|
|
160
|
+
test("uses default error message and type if only delay is provided", async () => {
|
|
161
|
+
try {
|
|
162
|
+
await delayedError(undefined, undefined, SHORT_DELAY)
|
|
163
|
+
} catch (error) {
|
|
164
|
+
expect(error.message).toBe("ERROR!")
|
|
165
|
+
expect(error).toBeInstanceOf(Error)
|
|
166
|
+
}
|
|
167
|
+
})
|
|
168
|
+
|
|
169
|
+
test("respects custom error types in delayed mode", async () => {
|
|
170
|
+
try {
|
|
171
|
+
await delayedError("Type fail", ErrorType.TypeError, SHORT_DELAY)
|
|
172
|
+
} catch (error) {
|
|
173
|
+
expect(error).toBeInstanceOf(TypeError)
|
|
174
|
+
expect(error.message).toBe("Type fail")
|
|
175
|
+
}
|
|
176
|
+
})
|
|
177
|
+
})
|
|
178
|
+
|
|
179
|
+
describe("throwWhatever utility", () => {
|
|
180
|
+
test("throws a standard error object when passed", () => {
|
|
181
|
+
const err = new Error("standard fail")
|
|
182
|
+
expect(() => throwWhatever(err)).toThrow("standard fail")
|
|
183
|
+
})
|
|
184
|
+
|
|
185
|
+
test("throws primitive false when passed false", () => {
|
|
186
|
+
try {
|
|
187
|
+
throwWhatever(false)
|
|
188
|
+
} catch (e) {
|
|
189
|
+
expect(e).toBe(false)
|
|
190
|
+
}
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
test("throws primitive zero when passed 0", () => {
|
|
194
|
+
try {
|
|
195
|
+
throwWhatever(0)
|
|
196
|
+
} catch (e) {
|
|
197
|
+
expect(e).toBe(0)
|
|
198
|
+
}
|
|
199
|
+
})
|
|
200
|
+
|
|
201
|
+
test("throws a string directly when passed a string", () => {
|
|
202
|
+
expect(() => throwWhatever("Direct String")).toThrow("Direct String")
|
|
203
|
+
})
|
|
204
|
+
|
|
205
|
+
test("integrates with getError for custom domain errors", () => {
|
|
206
|
+
const FruitError = getError(ErrorType.FruitConsumptionError)
|
|
207
|
+
const err = new FruitError("Too many apples")
|
|
208
|
+
expect(() => throwWhatever(err)).toThrow(FruitError)
|
|
209
|
+
})
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
describe("getError utility", () => {
|
|
213
|
+
test("returns the intrinsic Error constructor for BaseError", () => {
|
|
214
|
+
const result = getError(ErrorType.BaseError)
|
|
215
|
+
expect(result).toBe(Error)
|
|
216
|
+
})
|
|
217
|
+
|
|
218
|
+
test("returns the intrinsic TypeError constructor for TypeError", () => {
|
|
219
|
+
const result = getError(ErrorType.TypeError)
|
|
220
|
+
expect(result).toBe(TypeError)
|
|
221
|
+
})
|
|
222
|
+
|
|
223
|
+
test("successfully extracts FruitConsumptionError from jsfruit logic", () => {
|
|
224
|
+
const FruitError = getError(ErrorType.FruitConsumptionError)
|
|
225
|
+
expect(typeof FruitError).toBe("function")
|
|
226
|
+
const instance = new FruitError("test")
|
|
227
|
+
expect(instance).toBeInstanceOf(Error)
|
|
228
|
+
})
|
|
229
|
+
|
|
230
|
+
test("successfully extracts PersonNotHungryError from libperson logic", () => {
|
|
231
|
+
const HungerError = getError(ErrorType.PersonNotHungryError)
|
|
232
|
+
expect(typeof HungerError).toBe("function")
|
|
233
|
+
expect(new HungerError()).toBeDefined()
|
|
234
|
+
})
|
|
235
|
+
|
|
236
|
+
test("successfully extracts PortionsError from libguacamole logic", () => {
|
|
237
|
+
const GuacError = getError(ErrorType.PortionsError)
|
|
238
|
+
expect(typeof GuacError).toBe("function")
|
|
239
|
+
expect(GuacError.name).toBe("PortionsError")
|
|
240
|
+
})
|
|
241
|
+
|
|
242
|
+
test("successfully extracts FalseJSValidationFailedToPassError", () => {
|
|
243
|
+
const FalseJSValidationFailedToPassError = getError(ErrorType.FalseJSValidationFailedToPassError)
|
|
244
|
+
expect(typeof FalseJSValidationFailedToPassError).toBe("function")
|
|
245
|
+
expect(FalseJSValidationFailedToPassError.name).toBe("FalseJSValidationFailedToPassError")
|
|
246
|
+
})
|
|
247
|
+
|
|
248
|
+
test("returns the same constructor when passed a constructor (identity)", () => {
|
|
249
|
+
class MyError extends Error {}
|
|
250
|
+
const result = getError(MyError)
|
|
251
|
+
expect(result).toBe(MyError)
|
|
252
|
+
})
|
|
253
|
+
})
|
|
254
|
+
|
|
255
|
+
describe("error messages", () => {
|
|
256
|
+
test.each([
|
|
257
|
+
["no fruit left", MESSAGES.DOMAIN.FRUIT_CONSUMPTION_ERROR.NO_FRUIT_LEFT],
|
|
258
|
+
["vegetables can not talk", MESSAGES.DOMAIN.VEGETABLES_DO_NOT_TALK_ERROR.VEGETABLES_CAN_NOT_TALK],
|
|
259
|
+
["% is not hungry and cannot be fed", MESSAGES.DOMAIN.PERSON_NOT_HUNGRY_ERROR.IS_NOT_HUNGRY_AND_CANNOT_BE_FED],
|
|
260
|
+
["Portion size expected to be a positive integer", MESSAGES.DOMAIN.PORTIONS_ERROR.PORTION_SIZE_EXPECTED_TO_BE_A_POSITIVE_INTEGER],
|
|
261
|
+
["Too many portions", MESSAGES.DOMAIN.PORTIONS_ERROR.TOO_MANY_PORTIONS],
|
|
262
|
+
["Validation failed to pass", MESSAGES.DOMAIN.FALSEJS_VALIDATION_FAILED_TO_PASS_ERROR.VALIDATION_FAILED_TO_PASS]
|
|
263
|
+
])("provides error message \"%s\" correctly", (a, b) => {
|
|
264
|
+
expect(a).toEqual(b)
|
|
265
|
+
})
|
|
266
|
+
})
|