immediate-error 11.1.3 → 12.1.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 +21 -0
- package/index.d.ts +2 -0
- package/index.js +147 -47
- package/index.test.js +87 -10
- package/jest.config.js +12 -0
- package/package.json +24 -5
package/README.md
CHANGED
|
@@ -38,6 +38,8 @@ immediateError("vegetables cannot talk error", ErrorType.VegetablesCannotTalkErr
|
|
|
38
38
|
|
|
39
39
|
immediateError("person not hungry error", ErrorType.PersonNotHungryError) // throws a PersonNotHungryError
|
|
40
40
|
|
|
41
|
+
immediateError("portions error", ErrorType.PortionsError) // throws a PortionsError
|
|
42
|
+
|
|
41
43
|
class MyCustomError extends Error {
|
|
42
44
|
constructor (message) {
|
|
43
45
|
super("Custom: " + message)
|
|
@@ -96,5 +98,24 @@ const { delayedError, ErrorType } = require("immediate-error")
|
|
|
96
98
|
delayedError("delayed", ErrorType.BaseError, 1000) // waits 1000 ms (1 second) and then throws error
|
|
97
99
|
```
|
|
98
100
|
|
|
101
|
+
## `immediate-error` 2.0.0
|
|
102
|
+
|
|
103
|
+
If you are looking for a more lightweight alternative to `immediate-error`'s latest version that has only 2 dependencies, you can use `immediate-error` version 2.0.0:
|
|
104
|
+
|
|
105
|
+
Install:
|
|
106
|
+
```bash
|
|
107
|
+
npm install immediate-error@2.0.0
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Use:
|
|
111
|
+
```js
|
|
112
|
+
const throwErrorWithMessage = require("immediate-error")
|
|
113
|
+
|
|
114
|
+
throwErrorWithMessage("Test") // throws an error with message Test
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
It always throws a base error with the message passed in.
|
|
118
|
+
|
|
119
|
+
|
|
99
120
|
## License
|
|
100
121
|
Unlicense
|
package/index.d.ts
CHANGED
|
@@ -9,6 +9,7 @@ export enum ErrorType {
|
|
|
9
9
|
FruitConsumptionError = 7,
|
|
10
10
|
VegetablesCannotTalkError = 8,
|
|
11
11
|
PersonNotHungryError = 9,
|
|
12
|
+
PortionsError = 10
|
|
12
13
|
}
|
|
13
14
|
|
|
14
15
|
export type CustomError = {
|
|
@@ -31,6 +32,7 @@ export function getError(
|
|
|
31
32
|
errorType: ErrorType.VegetablesCannotTalkError
|
|
32
33
|
): CustomError
|
|
33
34
|
export function getError(errorType: ErrorType.PersonNotHungryError): CustomError
|
|
35
|
+
export function getError(errorType: ErrorType.PortionsError): CustomError
|
|
34
36
|
export function getError(errorType: ErrorType | CustomError): CustomError
|
|
35
37
|
|
|
36
38
|
export function immediateError(
|
package/index.js
CHANGED
|
@@ -10,8 +10,10 @@ let interpret = require("javascript-interpreter/interpret")
|
|
|
10
10
|
const Fruit = require("jsfruit")
|
|
11
11
|
const Vegetable = require("libvegetable")
|
|
12
12
|
const Person = require("libperson")
|
|
13
|
+
const Guacamole = require("libguacamole")
|
|
14
|
+
const isEqual = require("@10xly/strict-equals")
|
|
13
15
|
const isdash = require("is-")
|
|
14
|
-
const noop = require("
|
|
16
|
+
const noop = require("noop-enterprise")
|
|
15
17
|
const noop10 = require("noop10")
|
|
16
18
|
const bail = require("bail")
|
|
17
19
|
const construct = require("construct-new")
|
|
@@ -24,12 +26,13 @@ const repeating = require("repeating")
|
|
|
24
26
|
const deepFreeze = require("deep-freeze-node3") // 3rd iteration of deep-freeze-node, and the only 10x one.
|
|
25
27
|
const concat = require("@rightpad/concat")
|
|
26
28
|
const NEWLINE = require("fizzbuzz-enterprise/source/main/constants/strings/delimiters/Newline") // hax
|
|
29
|
+
const emptyString = require("empty-string")
|
|
27
30
|
const falseValue = require("false-value")
|
|
28
31
|
const sleep = require("@redux-saga/delay-p").default
|
|
29
32
|
const uncurry = require("uncurry-x")
|
|
30
33
|
const join = require("array.prototype.join")
|
|
31
34
|
const at = require("string.prototype.at")
|
|
32
|
-
const replaceAll = require("
|
|
35
|
+
const replaceAll = require("str-replaceallof-es")
|
|
33
36
|
const split = require("string.prototype.split")
|
|
34
37
|
const length = require("utf8-byte-length")
|
|
35
38
|
const call = require("node-call.then")
|
|
@@ -41,14 +44,16 @@ const stubArray = require("lodash.stubarray")
|
|
|
41
44
|
const arrayGetMember = uncurry(require("array-get-member").arrayGetMember)
|
|
42
45
|
require("get-member")()
|
|
43
46
|
const objGetMember = uncurry(require("object.prototype-intrinsic-ai").getMember)
|
|
44
|
-
delete require("object.prototype-intrinsic-ai").getMember
|
|
45
47
|
const ParseFloat = require("numero").parseFloat
|
|
46
48
|
const unicodePo = require("unicode/category/Po")
|
|
47
49
|
const subtract = require("subtract")
|
|
48
50
|
const forEach = require("for-each")
|
|
49
51
|
const head = require("@extra-array/head")
|
|
50
52
|
const last = require("@extra-array/last")
|
|
51
|
-
const unicodePoArray = objGetMember(
|
|
53
|
+
const unicodePoArray = objGetMember(
|
|
54
|
+
just,
|
|
55
|
+
"call",
|
|
56
|
+
)(function () {
|
|
52
57
|
const unicodePoArray = stubArray()
|
|
53
58
|
forEach(entries(unicodePo), (entry) => {
|
|
54
59
|
const key = head(entry)
|
|
@@ -59,8 +64,23 @@ const unicodePoArray = objGetMember(just, "call")(function () {
|
|
|
59
64
|
return unicodePoArray
|
|
60
65
|
})
|
|
61
66
|
const coalesce = require("es-logical-nullish-coalescing-operator")
|
|
67
|
+
const isFalse = require("@is-(unknown)/is-false")
|
|
68
|
+
const isPositiveZero = require("positive-zero")
|
|
69
|
+
const isNegativeZero = require("@is-(unknown)/is-negative-zero")
|
|
70
|
+
const isNullOrUndefined = require("@is-(unknown)/is-nil")
|
|
71
|
+
const isNull = require("@is-(unknown)/is-null")
|
|
72
|
+
const isUndefined = require("@is-(unknown)/is-undefined")
|
|
73
|
+
const isNaN = require("@is-(unknown)/is-nan")
|
|
62
74
|
|
|
75
|
+
const nullvalue = require("primitive-value-null")
|
|
76
|
+
const nanvalue = require("primitive-value-nan")
|
|
77
|
+
const undef = require("primitive-value-undefined")
|
|
78
|
+
|
|
79
|
+
const $BigInt = require("bigint-intrinsic-ai")
|
|
80
|
+
|
|
81
|
+
const negativeZero = require("@negative-numbers/zero")
|
|
63
82
|
const zero = require("@positive-numbers/zero")
|
|
83
|
+
const zeroBigint = $BigInt(zero)
|
|
64
84
|
const one = require("@positive-numbers/one")
|
|
65
85
|
const two = require("@positive-numbers/two")
|
|
66
86
|
const three = require("@positive-numbers/three")
|
|
@@ -70,12 +90,14 @@ const six = require("@positive-numbers/six")
|
|
|
70
90
|
const seven = require("@positive-numbers/seven")
|
|
71
91
|
const eight = require("@positive-numbers/eight")
|
|
72
92
|
const nine = require("@positive-numbers/nine")
|
|
93
|
+
const ten = require("@positive-numbers/ten")
|
|
73
94
|
const eleven = require("@positive-numbers/eleven")
|
|
74
95
|
const seventeen = require("@positive-numbers/seventeen")
|
|
75
96
|
const twentyFive = require("@positive-numbers/twenty-five")
|
|
76
97
|
const twentyNine = require("@positive-numbers/twenty-nine")
|
|
77
98
|
const thirtyThree = require("@positive-numbers/thirty-three")
|
|
78
99
|
const oneHundred = require("fizzbuzz-enterprise/source/main/constants/magic-numbers/Hundred")
|
|
100
|
+
const negative87 = require("@negative-numbers/eighty-seven")
|
|
79
101
|
|
|
80
102
|
const E = require("@uppercase-letters/e")
|
|
81
103
|
const O = require("@uppercase-letters/o")
|
|
@@ -105,6 +127,7 @@ const ErrorType = deepFreeze({
|
|
|
105
127
|
FruitConsumptionError: seven,
|
|
106
128
|
VegetablesCannotTalkError: eight,
|
|
107
129
|
PersonNotHungryError: nine,
|
|
130
|
+
PortionsError: ten,
|
|
108
131
|
})
|
|
109
132
|
|
|
110
133
|
const ErrorMap = construct({
|
|
@@ -120,14 +143,14 @@ function target_(value) {
|
|
|
120
143
|
}),
|
|
121
144
|
at(toStr(noop10), one),
|
|
122
145
|
at(toStr(noop10), zero),
|
|
123
|
-
at(toStr(noop10), one)
|
|
146
|
+
at(toStr(noop10), one),
|
|
124
147
|
)
|
|
125
148
|
}
|
|
126
149
|
function createObjectWithTargetKey(value) {
|
|
127
150
|
let string = toStr(target_)
|
|
128
151
|
string = arrayGetMember(
|
|
129
152
|
split(string, toStr(target_).substr(zero, twentyFive)),
|
|
130
|
-
one
|
|
153
|
+
one,
|
|
131
154
|
)
|
|
132
155
|
string = string.substr(one)
|
|
133
156
|
string = string.substr(one)
|
|
@@ -139,16 +162,21 @@ function createObjectWithTargetKey(value) {
|
|
|
139
162
|
string = replaceAll(
|
|
140
163
|
string,
|
|
141
164
|
toStr(target_).substr(seventeen, five),
|
|
142
|
-
isString(value) ? `\"${value}\"` : value.name
|
|
165
|
+
isString(value) ? `\"${value}\"` : value.name,
|
|
143
166
|
)
|
|
144
167
|
const array = split(string, toStr(target_).substr(twentyNine, six))
|
|
145
168
|
array.shift()
|
|
146
169
|
eval(require("javascript-interpreter"))
|
|
147
170
|
interpret = require("javascript-interpreter/interpret")
|
|
148
|
-
return interpret(
|
|
171
|
+
return interpret(
|
|
172
|
+
interpret(join(array, toStr(target_).substr(twentyNine, six))),
|
|
173
|
+
)
|
|
149
174
|
}
|
|
150
175
|
|
|
151
|
-
objGetMember(
|
|
176
|
+
objGetMember(
|
|
177
|
+
just,
|
|
178
|
+
"call",
|
|
179
|
+
)(function () {
|
|
152
180
|
ErrorMap.set(ErrorType.BaseError, $BaseError)
|
|
153
181
|
ErrorMap.set(ErrorType.EvalError, $EvalError)
|
|
154
182
|
ErrorMap.set(ErrorType.RangeError, $RangeError)
|
|
@@ -159,44 +187,98 @@ objGetMember(just, "call")(function () {
|
|
|
159
187
|
|
|
160
188
|
ErrorMap.set(
|
|
161
189
|
ErrorType.FruitConsumptionError,
|
|
162
|
-
objGetMember(
|
|
190
|
+
objGetMember(
|
|
191
|
+
just,
|
|
192
|
+
"call",
|
|
193
|
+
)(function () {
|
|
163
194
|
const fruit = construct(createObjectWithTargetKey(Fruit))
|
|
164
|
-
|
|
195
|
+
let result
|
|
196
|
+
attempt(() => {
|
|
165
197
|
fruit
|
|
166
198
|
eval(require("javascript-interpreter"))
|
|
167
199
|
interpret = require("javascript-interpreter/interpret")
|
|
168
200
|
|
|
169
201
|
interpret(repeating(concat("fruit.eat()", NEWLINE), eleven))
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
202
|
+
})
|
|
203
|
+
.rescue((error) => {
|
|
204
|
+
result = error.constructor
|
|
205
|
+
})
|
|
206
|
+
.else(noop)
|
|
207
|
+
.ensure(noop)
|
|
208
|
+
.end()
|
|
209
|
+
|
|
210
|
+
return result
|
|
211
|
+
}),
|
|
174
212
|
)
|
|
175
213
|
|
|
176
214
|
ErrorMap.set(
|
|
177
215
|
ErrorType.VegetablesCannotTalkError,
|
|
178
|
-
objGetMember(
|
|
216
|
+
objGetMember(
|
|
217
|
+
just,
|
|
218
|
+
"call",
|
|
219
|
+
)(function () {
|
|
179
220
|
const vegetable = construct(createObjectWithTargetKey(Vegetable))
|
|
221
|
+
let result
|
|
180
222
|
|
|
181
|
-
|
|
223
|
+
attempt(() => {
|
|
182
224
|
vegetable.greet()
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
225
|
+
})
|
|
226
|
+
.rescue((error) => {
|
|
227
|
+
result = error.constructor
|
|
228
|
+
})
|
|
229
|
+
.else(noop)
|
|
230
|
+
.ensure(noop)
|
|
231
|
+
.end()
|
|
232
|
+
|
|
233
|
+
return result
|
|
234
|
+
}),
|
|
187
235
|
)
|
|
188
236
|
|
|
189
237
|
ErrorMap.set(
|
|
190
238
|
ErrorType.PersonNotHungryError,
|
|
191
|
-
objGetMember(
|
|
239
|
+
objGetMember(
|
|
240
|
+
just,
|
|
241
|
+
"call",
|
|
242
|
+
)(function () {
|
|
192
243
|
const person = construct(createObjectWithTargetKey(Person))
|
|
193
244
|
person.hungry = falseValue()
|
|
194
|
-
|
|
245
|
+
let result
|
|
246
|
+
|
|
247
|
+
attempt(() => {
|
|
195
248
|
person.feed()
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
249
|
+
})
|
|
250
|
+
.rescue((error) => {
|
|
251
|
+
result = error.constructor
|
|
252
|
+
})
|
|
253
|
+
.else(noop)
|
|
254
|
+
.ensure(noop)
|
|
255
|
+
.end()
|
|
256
|
+
|
|
257
|
+
return result
|
|
258
|
+
}),
|
|
259
|
+
)
|
|
260
|
+
|
|
261
|
+
ErrorMap.set(
|
|
262
|
+
ErrorType.PortionsError,
|
|
263
|
+
objGetMember(
|
|
264
|
+
just,
|
|
265
|
+
"call",
|
|
266
|
+
)(function () {
|
|
267
|
+
let result
|
|
268
|
+
attempt(() => {
|
|
269
|
+
construct({
|
|
270
|
+
target: Guacamole,
|
|
271
|
+
args: [negative87],
|
|
272
|
+
})
|
|
273
|
+
})
|
|
274
|
+
.rescue((error) => {
|
|
275
|
+
result = error.constructor
|
|
276
|
+
})
|
|
277
|
+
.else(noop)
|
|
278
|
+
.ensure(noop)
|
|
279
|
+
.end()
|
|
280
|
+
return result
|
|
281
|
+
}),
|
|
200
282
|
)
|
|
201
283
|
})
|
|
202
284
|
|
|
@@ -205,18 +287,13 @@ function CreateSleepFunction(delay) {
|
|
|
205
287
|
}
|
|
206
288
|
|
|
207
289
|
function CreateError(error, message) {
|
|
208
|
-
return construct(
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
}
|
|
213
|
-
)
|
|
290
|
+
return construct({
|
|
291
|
+
target: error,
|
|
292
|
+
args: asArray(message),
|
|
293
|
+
})
|
|
214
294
|
}
|
|
215
295
|
|
|
216
|
-
exports.immediateError = function immediateError(
|
|
217
|
-
message,
|
|
218
|
-
errorType
|
|
219
|
-
) {
|
|
296
|
+
exports.immediateError = function immediateError(message, errorType) {
|
|
220
297
|
message = coalesce(message, default_error)
|
|
221
298
|
errorType = coalesce(errorType, ErrorType.BaseError)
|
|
222
299
|
var error
|
|
@@ -247,25 +324,48 @@ exports.immediateError = function immediateError(
|
|
|
247
324
|
require("is-not-integer")() // how did we get here?
|
|
248
325
|
}
|
|
249
326
|
|
|
250
|
-
exports.delayedError = function delayedError(
|
|
251
|
-
message = default_error
|
|
252
|
-
errorType = ErrorType.BaseError
|
|
253
|
-
|
|
254
|
-
)
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
327
|
+
exports.delayedError = function delayedError(message, errorType, delay) {
|
|
328
|
+
message = coalesce(message, default_error)
|
|
329
|
+
errorType = coalesce(errorType, ErrorType.BaseError)
|
|
330
|
+
return objGetMember(call, "then")(
|
|
331
|
+
objGetMember(just, "call")(CreateSleepFunction(delay)),
|
|
332
|
+
() => {
|
|
333
|
+
return exports.immediateError(message, errorType)
|
|
334
|
+
},
|
|
335
|
+
)
|
|
258
336
|
}
|
|
259
337
|
|
|
260
338
|
exports.getError = function getError(errorType) {
|
|
261
|
-
return ErrorMap.get(errorType)
|
|
339
|
+
return ErrorMap.get(errorType) ? ErrorMap.get(errorType) : errorType
|
|
262
340
|
}
|
|
263
341
|
|
|
264
342
|
exports.throwWhatever = function throwWhatever(whateverToThrow) {
|
|
265
343
|
if (whateverToThrow) {
|
|
266
344
|
bail(whateverToThrow)
|
|
267
345
|
} else {
|
|
268
|
-
|
|
346
|
+
if (isFalse(whateverToThrow)) {
|
|
347
|
+
just.throw(falseValue())
|
|
348
|
+
} else if (isPositiveZero(whateverToThrow)) {
|
|
349
|
+
just.throw(zero)
|
|
350
|
+
} else if (isNegativeZero(whateverToThrow)) {
|
|
351
|
+
just.throw(negativeZero)
|
|
352
|
+
} else if (isEqual(whateverToThrow, zeroBigint)) {
|
|
353
|
+
just.throw(zeroBigint)
|
|
354
|
+
} else if (isEqual(whateverToThrow, emptyString)) {
|
|
355
|
+
just.throw(emptyString)
|
|
356
|
+
} else if (isNullOrUndefined(whateverToThrow)) {
|
|
357
|
+
if (isNull(whateverToThrow)) {
|
|
358
|
+
just.throw(nullvalue)
|
|
359
|
+
} else if (isUndefined(whateverToThrow)) {
|
|
360
|
+
just.throw(undef)
|
|
361
|
+
} else {
|
|
362
|
+
just.throw(CreateError($BaseError, "THE WORLD IS ENDING"))
|
|
363
|
+
}
|
|
364
|
+
} else if (isNaN(whateverToThrow)) {
|
|
365
|
+
just.throw(nanvalue)
|
|
366
|
+
} else {
|
|
367
|
+
just.throw(whateverToThrow) // throw
|
|
368
|
+
}
|
|
269
369
|
}
|
|
270
370
|
}
|
|
271
371
|
|
package/index.test.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
const {
|
|
1
|
+
const {
|
|
2
|
+
immediateError,
|
|
3
|
+
ErrorType,
|
|
4
|
+
throwWhatever,
|
|
5
|
+
getError,
|
|
6
|
+
} = require("./index")
|
|
2
7
|
|
|
3
8
|
describe("immediateError utility", () => {
|
|
4
|
-
|
|
5
9
|
test("throws a regular Error with default message when no arguments are passed", () => {
|
|
6
10
|
expect(() => immediateError()).toThrow(Error)
|
|
7
11
|
expect(() => immediateError()).toThrow("ERROR!")
|
|
@@ -24,12 +28,13 @@ describe("immediateError utility", () => {
|
|
|
24
28
|
expect(() => immediateError("test message", type)).toThrow(constructor)
|
|
25
29
|
})
|
|
26
30
|
|
|
27
|
-
|
|
31
|
+
test.each([
|
|
28
32
|
["FruitConsumptionError", ErrorType.FruitConsumptionError],
|
|
29
33
|
["VegetablesCannotTalkError", ErrorType.VegetablesCannotTalkError],
|
|
30
34
|
["PersonNotHungryError", ErrorType.PersonNotHungryError],
|
|
35
|
+
["PortionsError", ErrorType.PortionsError],
|
|
31
36
|
])("throws domain-specific %s correctly", (name, type) => {
|
|
32
|
-
const expectedConstructor =
|
|
37
|
+
const expectedConstructor = getError(type)
|
|
33
38
|
expect(() => immediateError("enterprise failure", type)).toThrow(expectedConstructor)
|
|
34
39
|
expect(() => immediateError("enterprise failure", type)).toThrow("enterprise failure")
|
|
35
40
|
})
|
|
@@ -43,7 +48,9 @@ describe("immediateError utility", () => {
|
|
|
43
48
|
}
|
|
44
49
|
|
|
45
50
|
expect(() => immediateError("Error!", MyCustomError)).toThrow(MyCustomError)
|
|
46
|
-
expect(() => immediateError("Error!", MyCustomError)).toThrow(
|
|
51
|
+
expect(() => immediateError("Error!", MyCustomError)).toThrow(
|
|
52
|
+
"Custom: Error!",
|
|
53
|
+
)
|
|
47
54
|
})
|
|
48
55
|
|
|
49
56
|
test("captures stack trace correctly and hides internal frames", () => {
|
|
@@ -99,13 +106,13 @@ describe("attempt utility", () => {
|
|
|
99
106
|
|
|
100
107
|
test("triggers ensure regardless of success or failure", () => {
|
|
101
108
|
let counter = 0
|
|
102
|
-
|
|
109
|
+
|
|
103
110
|
attempt(() => {})
|
|
104
111
|
.ensure(() => {
|
|
105
112
|
counter++
|
|
106
113
|
})
|
|
107
114
|
.end()
|
|
108
|
-
|
|
115
|
+
|
|
109
116
|
attempt(() => {
|
|
110
117
|
throw new Error()
|
|
111
118
|
})
|
|
@@ -123,7 +130,7 @@ describe("attempt utility", () => {
|
|
|
123
130
|
const b = a.rescue(() => {})
|
|
124
131
|
const c = b.else(() => {})
|
|
125
132
|
const d = c.ensure(() => {})
|
|
126
|
-
|
|
133
|
+
|
|
127
134
|
expect(a).toBe(d)
|
|
128
135
|
})
|
|
129
136
|
})
|
|
@@ -135,7 +142,7 @@ describe("delayedError utility", () => {
|
|
|
135
142
|
|
|
136
143
|
test("throws the error after a specified delay", async () => {
|
|
137
144
|
const start = Date.now()
|
|
138
|
-
|
|
145
|
+
|
|
139
146
|
try {
|
|
140
147
|
await delayedError("Delayed fail", ErrorType.BaseError, SHORT_DELAY)
|
|
141
148
|
} catch (error) {
|
|
@@ -162,4 +169,74 @@ describe("delayedError utility", () => {
|
|
|
162
169
|
expect(error.message).toBe("Type fail")
|
|
163
170
|
}
|
|
164
171
|
})
|
|
165
|
-
})
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
describe("throwWhatever utility", () => {
|
|
175
|
+
test("throws a standard error object when passed", () => {
|
|
176
|
+
const err = new Error("standard fail")
|
|
177
|
+
expect(() => throwWhatever(err)).toThrow("standard fail")
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
test("throws primitive false when passed false", () => {
|
|
181
|
+
try {
|
|
182
|
+
throwWhatever(false)
|
|
183
|
+
} catch (e) {
|
|
184
|
+
expect(e).toBe(false)
|
|
185
|
+
}
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
test("throws primitive zero when passed 0", () => {
|
|
189
|
+
try {
|
|
190
|
+
throwWhatever(0)
|
|
191
|
+
} catch (e) {
|
|
192
|
+
expect(e).toBe(0)
|
|
193
|
+
}
|
|
194
|
+
})
|
|
195
|
+
|
|
196
|
+
test("throws a string directly when passed a string", () => {
|
|
197
|
+
expect(() => throwWhatever("Direct String")).toThrow("Direct String")
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
test("integrates with getError for custom domain errors", () => {
|
|
201
|
+
const FruitError = getError(ErrorType.FruitConsumptionError)
|
|
202
|
+
const err = new FruitError("Too many apples")
|
|
203
|
+
expect(() => throwWhatever(err)).toThrow(FruitError)
|
|
204
|
+
})
|
|
205
|
+
})
|
|
206
|
+
|
|
207
|
+
describe("getError utility", () => {
|
|
208
|
+
test("returns the intrinsic Error constructor for BaseError", () => {
|
|
209
|
+
const result = getError(ErrorType.BaseError)
|
|
210
|
+
expect(result).toBe(Error)
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
test("returns the intrinsic TypeError constructor for TypeError", () => {
|
|
214
|
+
const result = getError(ErrorType.TypeError)
|
|
215
|
+
expect(result).toBe(TypeError)
|
|
216
|
+
})
|
|
217
|
+
|
|
218
|
+
test("successfully extracts FruitConsumptionError from jsfruit logic", () => {
|
|
219
|
+
const FruitError = getError(ErrorType.FruitConsumptionError)
|
|
220
|
+
expect(typeof FruitError).toBe("function")
|
|
221
|
+
const instance = new FruitError("test")
|
|
222
|
+
expect(instance).toBeInstanceOf(Error)
|
|
223
|
+
})
|
|
224
|
+
|
|
225
|
+
test("successfully extracts PersonNotHungryError from libperson logic", () => {
|
|
226
|
+
const HungerError = getError(ErrorType.PersonNotHungryError)
|
|
227
|
+
expect(typeof HungerError).toBe("function")
|
|
228
|
+
expect(new HungerError()).toBeDefined()
|
|
229
|
+
})
|
|
230
|
+
|
|
231
|
+
test("successfully extracts PortionsError from libguacamole logic", () => {
|
|
232
|
+
const GuacError = getError(ErrorType.PortionsError)
|
|
233
|
+
expect(typeof GuacError).toBe("function")
|
|
234
|
+
expect(GuacError.name).toBe("PortionsError")
|
|
235
|
+
})
|
|
236
|
+
|
|
237
|
+
test("returns the same constructor when passed a constructor (identity)", () => {
|
|
238
|
+
class MyError extends Error {}
|
|
239
|
+
const result = getError(MyError)
|
|
240
|
+
expect(result).toBe(MyError)
|
|
241
|
+
})
|
|
242
|
+
})
|
package/jest.config.js
ADDED
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "immediate-error",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "12.1.0",
|
|
4
4
|
"description": "enterprise errors",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"test": "jest
|
|
7
|
+
"test": "jest index.test.js"
|
|
8
8
|
},
|
|
9
9
|
"repository": {
|
|
10
10
|
"type": "git",
|
|
@@ -49,9 +49,19 @@
|
|
|
49
49
|
},
|
|
50
50
|
"homepage": "https://github.com/enterprise-npm-ai/immediate-error#readme",
|
|
51
51
|
"dependencies": {
|
|
52
|
+
"@10xly/strict-equals": "1.0.1",
|
|
53
|
+
"@characters/right-parenthesis": "2.1.0",
|
|
52
54
|
"@extra-array/head": "^2.10.19",
|
|
53
55
|
"@extra-array/last": "^2.10.19",
|
|
56
|
+
"@is-(unknown)/is-false": "1.5.0",
|
|
57
|
+
"@is-(unknown)/is-nan": "1.0.0",
|
|
58
|
+
"@is-(unknown)/is-negative-zero": "1.0.0",
|
|
59
|
+
"@is-(unknown)/is-nil": "1.2.0",
|
|
60
|
+
"@is-(unknown)/is-null": "1.3.0",
|
|
54
61
|
"@is-(unknown)/is-string": "^1.0.0",
|
|
62
|
+
"@is-(unknown)/is-undefined": "1.3.0",
|
|
63
|
+
"@negative-numbers/eighty-seven": "^1.0.0",
|
|
64
|
+
"@negative-numbers/zero": "1.0.0",
|
|
55
65
|
"@positive-numbers/eight": "^3.0.0",
|
|
56
66
|
"@positive-numbers/eleven": "^3.0.0",
|
|
57
67
|
"@positive-numbers/five": "^3.0.0",
|
|
@@ -61,6 +71,7 @@
|
|
|
61
71
|
"@positive-numbers/seven": "^3.0.0",
|
|
62
72
|
"@positive-numbers/seventeen": "^3.0.0",
|
|
63
73
|
"@positive-numbers/six": "^3.0.0",
|
|
74
|
+
"@positive-numbers/ten": "^3.0.0",
|
|
64
75
|
"@positive-numbers/thirty-three": "^3.0.0",
|
|
65
76
|
"@positive-numbers/three": "^3.0.0",
|
|
66
77
|
"@positive-numbers/twenty-five": "^4.0.0",
|
|
@@ -79,8 +90,10 @@
|
|
|
79
90
|
"attempt-statement": "^1.2.1",
|
|
80
91
|
"bail": "^1.0.5",
|
|
81
92
|
"basic-functions": "^1.0.6",
|
|
93
|
+
"bigint-intrinsic-ai": "1.0.0",
|
|
82
94
|
"construct-new": "^2.0.3",
|
|
83
95
|
"deep-freeze-node3": "^1.1.0",
|
|
96
|
+
"empty-string": "1.1.1",
|
|
84
97
|
"es-error-intrinsics": "^1.0.1",
|
|
85
98
|
"es-intrinsic-cache": "^1.0.1",
|
|
86
99
|
"es-logical-nullish-coalescing-operator": "^1.0.0",
|
|
@@ -93,20 +106,26 @@
|
|
|
93
106
|
"is-not-integer": "^1.0.2",
|
|
94
107
|
"javascript-interpreter": "^1.0.0",
|
|
95
108
|
"jsfruit": "^1.1.0",
|
|
109
|
+
"libguacamole": "^1.0.0",
|
|
96
110
|
"libperson": "^1.0.0",
|
|
97
111
|
"libvegetable": "^1.0.0",
|
|
98
112
|
"lodash.stubarray": "^4.13.0",
|
|
99
|
-
"n0p3-es2015-cjs": "^1.0.1",
|
|
100
113
|
"node-call.then": "^1.0.0",
|
|
101
|
-
"
|
|
114
|
+
"noop-enterprise": "^2.0.1",
|
|
115
|
+
"noop10": "1.0.3",
|
|
102
116
|
"numero": "^0.1.1",
|
|
103
117
|
"object.entries-ponyfill": "^1.0.1",
|
|
104
118
|
"object.prototype-intrinsic-ai": "^1.0.1",
|
|
119
|
+
"positive-zero": "3.0.0",
|
|
120
|
+
"primitive-value-nan": "1.0.1",
|
|
121
|
+
"primitive-value-null": "1.0.0",
|
|
122
|
+
"primitive-value-undefined": "1.0.0",
|
|
105
123
|
"qc-core": "^0.0.0",
|
|
106
124
|
"repeating": "^2.0.1",
|
|
107
125
|
"simple-lru-cache": "^0.0.2",
|
|
126
|
+
"str-replaceallof-es": "1.0.0",
|
|
108
127
|
"string.prototype.at": "^1.0.6",
|
|
109
|
-
"string.prototype.replaceall": "
|
|
128
|
+
"string.prototype.replaceall": "1.0.11",
|
|
110
129
|
"string.prototype.split": "^1.0.9",
|
|
111
130
|
"subtract": "^0.0.3",
|
|
112
131
|
"true-value": "^3.0.0",
|