immediate-error 12.0.0 → 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 +2 -0
- package/index.d.ts +2 -0
- package/index.js +28 -5
- package/index.test.js +23 -10
- package/package.json +4 -1
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)
|
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,6 +10,7 @@ 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")
|
|
13
14
|
const isEqual = require("@10xly/strict-equals")
|
|
14
15
|
const isdash = require("is-")
|
|
15
16
|
const noop = require("noop-enterprise")
|
|
@@ -89,12 +90,14 @@ const six = require("@positive-numbers/six")
|
|
|
89
90
|
const seven = require("@positive-numbers/seven")
|
|
90
91
|
const eight = require("@positive-numbers/eight")
|
|
91
92
|
const nine = require("@positive-numbers/nine")
|
|
93
|
+
const ten = require("@positive-numbers/ten")
|
|
92
94
|
const eleven = require("@positive-numbers/eleven")
|
|
93
95
|
const seventeen = require("@positive-numbers/seventeen")
|
|
94
96
|
const twentyFive = require("@positive-numbers/twenty-five")
|
|
95
97
|
const twentyNine = require("@positive-numbers/twenty-nine")
|
|
96
98
|
const thirtyThree = require("@positive-numbers/thirty-three")
|
|
97
99
|
const oneHundred = require("fizzbuzz-enterprise/source/main/constants/magic-numbers/Hundred")
|
|
100
|
+
const negative87 = require("@negative-numbers/eighty-seven")
|
|
98
101
|
|
|
99
102
|
const E = require("@uppercase-letters/e")
|
|
100
103
|
const O = require("@uppercase-letters/o")
|
|
@@ -124,6 +127,7 @@ const ErrorType = deepFreeze({
|
|
|
124
127
|
FruitConsumptionError: seven,
|
|
125
128
|
VegetablesCannotTalkError: eight,
|
|
126
129
|
PersonNotHungryError: nine,
|
|
130
|
+
PortionsError: ten,
|
|
127
131
|
})
|
|
128
132
|
|
|
129
133
|
const ErrorMap = construct({
|
|
@@ -253,6 +257,29 @@ objGetMember(
|
|
|
253
257
|
return result
|
|
254
258
|
}),
|
|
255
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
|
+
}),
|
|
282
|
+
)
|
|
256
283
|
})
|
|
257
284
|
|
|
258
285
|
function CreateSleepFunction(delay) {
|
|
@@ -297,11 +324,7 @@ exports.immediateError = function immediateError(message, errorType) {
|
|
|
297
324
|
require("is-not-integer")() // how did we get here?
|
|
298
325
|
}
|
|
299
326
|
|
|
300
|
-
exports.delayedError = function delayedError(
|
|
301
|
-
message,
|
|
302
|
-
errorType,
|
|
303
|
-
delay,
|
|
304
|
-
) {
|
|
327
|
+
exports.delayedError = function delayedError(message, errorType, delay) {
|
|
305
328
|
message = coalesce(message, default_error)
|
|
306
329
|
errorType = coalesce(errorType, ErrorType.BaseError)
|
|
307
330
|
return objGetMember(call, "then")(
|
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) {
|
|
@@ -221,9 +228,15 @@ describe("getError utility", () => {
|
|
|
221
228
|
expect(new HungerError()).toBeDefined()
|
|
222
229
|
})
|
|
223
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
|
+
|
|
224
237
|
test("returns the same constructor when passed a constructor (identity)", () => {
|
|
225
238
|
class MyError extends Error {}
|
|
226
239
|
const result = getError(MyError)
|
|
227
240
|
expect(result).toBe(MyError)
|
|
228
241
|
})
|
|
229
|
-
})
|
|
242
|
+
})
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "immediate-error",
|
|
3
|
-
"version": "12.
|
|
3
|
+
"version": "12.1.0",
|
|
4
4
|
"description": "enterprise errors",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -60,6 +60,7 @@
|
|
|
60
60
|
"@is-(unknown)/is-null": "1.3.0",
|
|
61
61
|
"@is-(unknown)/is-string": "^1.0.0",
|
|
62
62
|
"@is-(unknown)/is-undefined": "1.3.0",
|
|
63
|
+
"@negative-numbers/eighty-seven": "^1.0.0",
|
|
63
64
|
"@negative-numbers/zero": "1.0.0",
|
|
64
65
|
"@positive-numbers/eight": "^3.0.0",
|
|
65
66
|
"@positive-numbers/eleven": "^3.0.0",
|
|
@@ -70,6 +71,7 @@
|
|
|
70
71
|
"@positive-numbers/seven": "^3.0.0",
|
|
71
72
|
"@positive-numbers/seventeen": "^3.0.0",
|
|
72
73
|
"@positive-numbers/six": "^3.0.0",
|
|
74
|
+
"@positive-numbers/ten": "^3.0.0",
|
|
73
75
|
"@positive-numbers/thirty-three": "^3.0.0",
|
|
74
76
|
"@positive-numbers/three": "^3.0.0",
|
|
75
77
|
"@positive-numbers/twenty-five": "^4.0.0",
|
|
@@ -104,6 +106,7 @@
|
|
|
104
106
|
"is-not-integer": "^1.0.2",
|
|
105
107
|
"javascript-interpreter": "^1.0.0",
|
|
106
108
|
"jsfruit": "^1.1.0",
|
|
109
|
+
"libguacamole": "^1.0.0",
|
|
107
110
|
"libperson": "^1.0.0",
|
|
108
111
|
"libvegetable": "^1.0.0",
|
|
109
112
|
"lodash.stubarray": "^4.13.0",
|