immediate-error 11.1.2 → 12.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 +19 -0
- package/index.js +122 -45
- package/index.test.js +65 -1
- package/jest.config.js +12 -0
- package/package.json +21 -5
package/README.md
CHANGED
|
@@ -96,5 +96,24 @@ const { delayedError, ErrorType } = require("immediate-error")
|
|
|
96
96
|
delayedError("delayed", ErrorType.BaseError, 1000) // waits 1000 ms (1 second) and then throws error
|
|
97
97
|
```
|
|
98
98
|
|
|
99
|
+
## `immediate-error` 2.0.0
|
|
100
|
+
|
|
101
|
+
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:
|
|
102
|
+
|
|
103
|
+
Install:
|
|
104
|
+
```bash
|
|
105
|
+
npm install immediate-error@2.0.0
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Use:
|
|
109
|
+
```js
|
|
110
|
+
const throwErrorWithMessage = require("immediate-error")
|
|
111
|
+
|
|
112
|
+
throwErrorWithMessage("Test") // throws an error with message Test
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
It always throws a base error with the message passed in.
|
|
116
|
+
|
|
117
|
+
|
|
99
118
|
## License
|
|
100
119
|
Unlicense
|
package/index.js
CHANGED
|
@@ -10,8 +10,9 @@ 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 isEqual = require("@10xly/strict-equals")
|
|
13
14
|
const isdash = require("is-")
|
|
14
|
-
const noop = require("
|
|
15
|
+
const noop = require("noop-enterprise")
|
|
15
16
|
const noop10 = require("noop10")
|
|
16
17
|
const bail = require("bail")
|
|
17
18
|
const construct = require("construct-new")
|
|
@@ -24,12 +25,13 @@ const repeating = require("repeating")
|
|
|
24
25
|
const deepFreeze = require("deep-freeze-node3") // 3rd iteration of deep-freeze-node, and the only 10x one.
|
|
25
26
|
const concat = require("@rightpad/concat")
|
|
26
27
|
const NEWLINE = require("fizzbuzz-enterprise/source/main/constants/strings/delimiters/Newline") // hax
|
|
28
|
+
const emptyString = require("empty-string")
|
|
27
29
|
const falseValue = require("false-value")
|
|
28
30
|
const sleep = require("@redux-saga/delay-p").default
|
|
29
31
|
const uncurry = require("uncurry-x")
|
|
30
32
|
const join = require("array.prototype.join")
|
|
31
33
|
const at = require("string.prototype.at")
|
|
32
|
-
const replaceAll = require("
|
|
34
|
+
const replaceAll = require("str-replaceallof-es")
|
|
33
35
|
const split = require("string.prototype.split")
|
|
34
36
|
const length = require("utf8-byte-length")
|
|
35
37
|
const call = require("node-call.then")
|
|
@@ -41,14 +43,16 @@ const stubArray = require("lodash.stubarray")
|
|
|
41
43
|
const arrayGetMember = uncurry(require("array-get-member").arrayGetMember)
|
|
42
44
|
require("get-member")()
|
|
43
45
|
const objGetMember = uncurry(require("object.prototype-intrinsic-ai").getMember)
|
|
44
|
-
delete require("object.prototype-intrinsic-ai").getMember
|
|
45
46
|
const ParseFloat = require("numero").parseFloat
|
|
46
47
|
const unicodePo = require("unicode/category/Po")
|
|
47
48
|
const subtract = require("subtract")
|
|
48
49
|
const forEach = require("for-each")
|
|
49
50
|
const head = require("@extra-array/head")
|
|
50
51
|
const last = require("@extra-array/last")
|
|
51
|
-
const unicodePoArray = objGetMember(
|
|
52
|
+
const unicodePoArray = objGetMember(
|
|
53
|
+
just,
|
|
54
|
+
"call",
|
|
55
|
+
)(function () {
|
|
52
56
|
const unicodePoArray = stubArray()
|
|
53
57
|
forEach(entries(unicodePo), (entry) => {
|
|
54
58
|
const key = head(entry)
|
|
@@ -59,8 +63,23 @@ const unicodePoArray = objGetMember(just, "call")(function () {
|
|
|
59
63
|
return unicodePoArray
|
|
60
64
|
})
|
|
61
65
|
const coalesce = require("es-logical-nullish-coalescing-operator")
|
|
66
|
+
const isFalse = require("@is-(unknown)/is-false")
|
|
67
|
+
const isPositiveZero = require("positive-zero")
|
|
68
|
+
const isNegativeZero = require("@is-(unknown)/is-negative-zero")
|
|
69
|
+
const isNullOrUndefined = require("@is-(unknown)/is-nil")
|
|
70
|
+
const isNull = require("@is-(unknown)/is-null")
|
|
71
|
+
const isUndefined = require("@is-(unknown)/is-undefined")
|
|
72
|
+
const isNaN = require("@is-(unknown)/is-nan")
|
|
62
73
|
|
|
74
|
+
const nullvalue = require("primitive-value-null")
|
|
75
|
+
const nanvalue = require("primitive-value-nan")
|
|
76
|
+
const undef = require("primitive-value-undefined")
|
|
77
|
+
|
|
78
|
+
const $BigInt = require("bigint-intrinsic-ai")
|
|
79
|
+
|
|
80
|
+
const negativeZero = require("@negative-numbers/zero")
|
|
63
81
|
const zero = require("@positive-numbers/zero")
|
|
82
|
+
const zeroBigint = $BigInt(zero)
|
|
64
83
|
const one = require("@positive-numbers/one")
|
|
65
84
|
const two = require("@positive-numbers/two")
|
|
66
85
|
const three = require("@positive-numbers/three")
|
|
@@ -120,14 +139,14 @@ function target_(value) {
|
|
|
120
139
|
}),
|
|
121
140
|
at(toStr(noop10), one),
|
|
122
141
|
at(toStr(noop10), zero),
|
|
123
|
-
at(toStr(noop10), one)
|
|
142
|
+
at(toStr(noop10), one),
|
|
124
143
|
)
|
|
125
144
|
}
|
|
126
145
|
function createObjectWithTargetKey(value) {
|
|
127
146
|
let string = toStr(target_)
|
|
128
147
|
string = arrayGetMember(
|
|
129
148
|
split(string, toStr(target_).substr(zero, twentyFive)),
|
|
130
|
-
one
|
|
149
|
+
one,
|
|
131
150
|
)
|
|
132
151
|
string = string.substr(one)
|
|
133
152
|
string = string.substr(one)
|
|
@@ -139,16 +158,21 @@ function createObjectWithTargetKey(value) {
|
|
|
139
158
|
string = replaceAll(
|
|
140
159
|
string,
|
|
141
160
|
toStr(target_).substr(seventeen, five),
|
|
142
|
-
isString(value) ? `\"${value}\"` : value.name
|
|
161
|
+
isString(value) ? `\"${value}\"` : value.name,
|
|
143
162
|
)
|
|
144
163
|
const array = split(string, toStr(target_).substr(twentyNine, six))
|
|
145
164
|
array.shift()
|
|
146
165
|
eval(require("javascript-interpreter"))
|
|
147
166
|
interpret = require("javascript-interpreter/interpret")
|
|
148
|
-
return interpret(
|
|
167
|
+
return interpret(
|
|
168
|
+
interpret(join(array, toStr(target_).substr(twentyNine, six))),
|
|
169
|
+
)
|
|
149
170
|
}
|
|
150
171
|
|
|
151
|
-
objGetMember(
|
|
172
|
+
objGetMember(
|
|
173
|
+
just,
|
|
174
|
+
"call",
|
|
175
|
+
)(function () {
|
|
152
176
|
ErrorMap.set(ErrorType.BaseError, $BaseError)
|
|
153
177
|
ErrorMap.set(ErrorType.EvalError, $EvalError)
|
|
154
178
|
ErrorMap.set(ErrorType.RangeError, $RangeError)
|
|
@@ -159,44 +183,75 @@ objGetMember(just, "call")(function () {
|
|
|
159
183
|
|
|
160
184
|
ErrorMap.set(
|
|
161
185
|
ErrorType.FruitConsumptionError,
|
|
162
|
-
objGetMember(
|
|
186
|
+
objGetMember(
|
|
187
|
+
just,
|
|
188
|
+
"call",
|
|
189
|
+
)(function () {
|
|
163
190
|
const fruit = construct(createObjectWithTargetKey(Fruit))
|
|
164
|
-
|
|
191
|
+
let result
|
|
192
|
+
attempt(() => {
|
|
165
193
|
fruit
|
|
166
194
|
eval(require("javascript-interpreter"))
|
|
167
195
|
interpret = require("javascript-interpreter/interpret")
|
|
168
196
|
|
|
169
197
|
interpret(repeating(concat("fruit.eat()", NEWLINE), eleven))
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
198
|
+
})
|
|
199
|
+
.rescue((error) => {
|
|
200
|
+
result = error.constructor
|
|
201
|
+
})
|
|
202
|
+
.else(noop)
|
|
203
|
+
.ensure(noop)
|
|
204
|
+
.end()
|
|
205
|
+
|
|
206
|
+
return result
|
|
207
|
+
}),
|
|
174
208
|
)
|
|
175
209
|
|
|
176
210
|
ErrorMap.set(
|
|
177
211
|
ErrorType.VegetablesCannotTalkError,
|
|
178
|
-
objGetMember(
|
|
212
|
+
objGetMember(
|
|
213
|
+
just,
|
|
214
|
+
"call",
|
|
215
|
+
)(function () {
|
|
179
216
|
const vegetable = construct(createObjectWithTargetKey(Vegetable))
|
|
217
|
+
let result
|
|
180
218
|
|
|
181
|
-
|
|
219
|
+
attempt(() => {
|
|
182
220
|
vegetable.greet()
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
221
|
+
})
|
|
222
|
+
.rescue((error) => {
|
|
223
|
+
result = error.constructor
|
|
224
|
+
})
|
|
225
|
+
.else(noop)
|
|
226
|
+
.ensure(noop)
|
|
227
|
+
.end()
|
|
228
|
+
|
|
229
|
+
return result
|
|
230
|
+
}),
|
|
187
231
|
)
|
|
188
232
|
|
|
189
233
|
ErrorMap.set(
|
|
190
234
|
ErrorType.PersonNotHungryError,
|
|
191
|
-
objGetMember(
|
|
235
|
+
objGetMember(
|
|
236
|
+
just,
|
|
237
|
+
"call",
|
|
238
|
+
)(function () {
|
|
192
239
|
const person = construct(createObjectWithTargetKey(Person))
|
|
193
240
|
person.hungry = falseValue()
|
|
194
|
-
|
|
241
|
+
let result
|
|
242
|
+
|
|
243
|
+
attempt(() => {
|
|
195
244
|
person.feed()
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
245
|
+
})
|
|
246
|
+
.rescue((error) => {
|
|
247
|
+
result = error.constructor
|
|
248
|
+
})
|
|
249
|
+
.else(noop)
|
|
250
|
+
.ensure(noop)
|
|
251
|
+
.end()
|
|
252
|
+
|
|
253
|
+
return result
|
|
254
|
+
}),
|
|
200
255
|
)
|
|
201
256
|
})
|
|
202
257
|
|
|
@@ -205,18 +260,13 @@ function CreateSleepFunction(delay) {
|
|
|
205
260
|
}
|
|
206
261
|
|
|
207
262
|
function CreateError(error, message) {
|
|
208
|
-
return construct(
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
}
|
|
213
|
-
)
|
|
263
|
+
return construct({
|
|
264
|
+
target: error,
|
|
265
|
+
args: asArray(message),
|
|
266
|
+
})
|
|
214
267
|
}
|
|
215
268
|
|
|
216
|
-
exports.immediateError = function immediateError(
|
|
217
|
-
message,
|
|
218
|
-
errorType
|
|
219
|
-
) {
|
|
269
|
+
exports.immediateError = function immediateError(message, errorType) {
|
|
220
270
|
message = coalesce(message, default_error)
|
|
221
271
|
errorType = coalesce(errorType, ErrorType.BaseError)
|
|
222
272
|
var error
|
|
@@ -248,24 +298,51 @@ exports.immediateError = function immediateError(
|
|
|
248
298
|
}
|
|
249
299
|
|
|
250
300
|
exports.delayedError = function delayedError(
|
|
251
|
-
message
|
|
252
|
-
errorType
|
|
253
|
-
delay
|
|
301
|
+
message,
|
|
302
|
+
errorType,
|
|
303
|
+
delay,
|
|
254
304
|
) {
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
305
|
+
message = coalesce(message, default_error)
|
|
306
|
+
errorType = coalesce(errorType, ErrorType.BaseError)
|
|
307
|
+
return objGetMember(call, "then")(
|
|
308
|
+
objGetMember(just, "call")(CreateSleepFunction(delay)),
|
|
309
|
+
() => {
|
|
310
|
+
return exports.immediateError(message, errorType)
|
|
311
|
+
},
|
|
312
|
+
)
|
|
258
313
|
}
|
|
259
314
|
|
|
260
315
|
exports.getError = function getError(errorType) {
|
|
261
|
-
return ErrorMap.get(errorType)
|
|
316
|
+
return ErrorMap.get(errorType) ? ErrorMap.get(errorType) : errorType
|
|
262
317
|
}
|
|
263
318
|
|
|
264
319
|
exports.throwWhatever = function throwWhatever(whateverToThrow) {
|
|
265
320
|
if (whateverToThrow) {
|
|
266
321
|
bail(whateverToThrow)
|
|
267
322
|
} else {
|
|
268
|
-
|
|
323
|
+
if (isFalse(whateverToThrow)) {
|
|
324
|
+
just.throw(falseValue())
|
|
325
|
+
} else if (isPositiveZero(whateverToThrow)) {
|
|
326
|
+
just.throw(zero)
|
|
327
|
+
} else if (isNegativeZero(whateverToThrow)) {
|
|
328
|
+
just.throw(negativeZero)
|
|
329
|
+
} else if (isEqual(whateverToThrow, zeroBigint)) {
|
|
330
|
+
just.throw(zeroBigint)
|
|
331
|
+
} else if (isEqual(whateverToThrow, emptyString)) {
|
|
332
|
+
just.throw(emptyString)
|
|
333
|
+
} else if (isNullOrUndefined(whateverToThrow)) {
|
|
334
|
+
if (isNull(whateverToThrow)) {
|
|
335
|
+
just.throw(nullvalue)
|
|
336
|
+
} else if (isUndefined(whateverToThrow)) {
|
|
337
|
+
just.throw(undef)
|
|
338
|
+
} else {
|
|
339
|
+
just.throw(CreateError($BaseError, "THE WORLD IS ENDING"))
|
|
340
|
+
}
|
|
341
|
+
} else if (isNaN(whateverToThrow)) {
|
|
342
|
+
just.throw(nanvalue)
|
|
343
|
+
} else {
|
|
344
|
+
just.throw(whateverToThrow) // throw
|
|
345
|
+
}
|
|
269
346
|
}
|
|
270
347
|
}
|
|
271
348
|
|
package/index.test.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const { immediateError, ErrorType } = require("./index")
|
|
1
|
+
const { immediateError, ErrorType, throwWhatever, getError } = require("./index")
|
|
2
2
|
|
|
3
3
|
describe("immediateError utility", () => {
|
|
4
4
|
|
|
@@ -162,4 +162,68 @@ describe("delayedError utility", () => {
|
|
|
162
162
|
expect(error.message).toBe("Type fail")
|
|
163
163
|
}
|
|
164
164
|
})
|
|
165
|
+
})
|
|
166
|
+
|
|
167
|
+
describe("throwWhatever utility", () => {
|
|
168
|
+
test("throws a standard error object when passed", () => {
|
|
169
|
+
const err = new Error("standard fail")
|
|
170
|
+
expect(() => throwWhatever(err)).toThrow("standard fail")
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
test("throws primitive false when passed false", () => {
|
|
174
|
+
try {
|
|
175
|
+
throwWhatever(false)
|
|
176
|
+
} catch (e) {
|
|
177
|
+
expect(e).toBe(false)
|
|
178
|
+
}
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
test("throws primitive zero when passed 0", () => {
|
|
182
|
+
try {
|
|
183
|
+
throwWhatever(0)
|
|
184
|
+
} catch (e) {
|
|
185
|
+
expect(e).toBe(0)
|
|
186
|
+
}
|
|
187
|
+
})
|
|
188
|
+
|
|
189
|
+
test("throws a string directly when passed a string", () => {
|
|
190
|
+
expect(() => throwWhatever("Direct String")).toThrow("Direct String")
|
|
191
|
+
})
|
|
192
|
+
|
|
193
|
+
test("integrates with getError for custom domain errors", () => {
|
|
194
|
+
const FruitError = getError(ErrorType.FruitConsumptionError)
|
|
195
|
+
const err = new FruitError("Too many apples")
|
|
196
|
+
expect(() => throwWhatever(err)).toThrow(FruitError)
|
|
197
|
+
})
|
|
198
|
+
})
|
|
199
|
+
|
|
200
|
+
describe("getError utility", () => {
|
|
201
|
+
test("returns the intrinsic Error constructor for BaseError", () => {
|
|
202
|
+
const result = getError(ErrorType.BaseError)
|
|
203
|
+
expect(result).toBe(Error)
|
|
204
|
+
})
|
|
205
|
+
|
|
206
|
+
test("returns the intrinsic TypeError constructor for TypeError", () => {
|
|
207
|
+
const result = getError(ErrorType.TypeError)
|
|
208
|
+
expect(result).toBe(TypeError)
|
|
209
|
+
})
|
|
210
|
+
|
|
211
|
+
test("successfully extracts FruitConsumptionError from jsfruit logic", () => {
|
|
212
|
+
const FruitError = getError(ErrorType.FruitConsumptionError)
|
|
213
|
+
expect(typeof FruitError).toBe("function")
|
|
214
|
+
const instance = new FruitError("test")
|
|
215
|
+
expect(instance).toBeInstanceOf(Error)
|
|
216
|
+
})
|
|
217
|
+
|
|
218
|
+
test("successfully extracts PersonNotHungryError from libperson logic", () => {
|
|
219
|
+
const HungerError = getError(ErrorType.PersonNotHungryError)
|
|
220
|
+
expect(typeof HungerError).toBe("function")
|
|
221
|
+
expect(new HungerError()).toBeDefined()
|
|
222
|
+
})
|
|
223
|
+
|
|
224
|
+
test("returns the same constructor when passed a constructor (identity)", () => {
|
|
225
|
+
class MyError extends Error {}
|
|
226
|
+
const result = getError(MyError)
|
|
227
|
+
expect(result).toBe(MyError)
|
|
228
|
+
})
|
|
165
229
|
})
|
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.0.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,18 @@
|
|
|
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/zero": "1.0.0",
|
|
55
64
|
"@positive-numbers/eight": "^3.0.0",
|
|
56
65
|
"@positive-numbers/eleven": "^3.0.0",
|
|
57
66
|
"@positive-numbers/five": "^3.0.0",
|
|
@@ -79,8 +88,10 @@
|
|
|
79
88
|
"attempt-statement": "^1.2.1",
|
|
80
89
|
"bail": "^1.0.5",
|
|
81
90
|
"basic-functions": "^1.0.6",
|
|
91
|
+
"bigint-intrinsic-ai": "1.0.0",
|
|
82
92
|
"construct-new": "^2.0.3",
|
|
83
93
|
"deep-freeze-node3": "^1.1.0",
|
|
94
|
+
"empty-string": "1.1.1",
|
|
84
95
|
"es-error-intrinsics": "^1.0.1",
|
|
85
96
|
"es-intrinsic-cache": "^1.0.1",
|
|
86
97
|
"es-logical-nullish-coalescing-operator": "^1.0.0",
|
|
@@ -96,17 +107,22 @@
|
|
|
96
107
|
"libperson": "^1.0.0",
|
|
97
108
|
"libvegetable": "^1.0.0",
|
|
98
109
|
"lodash.stubarray": "^4.13.0",
|
|
99
|
-
"n0p3-es2015-cjs": "^1.0.1",
|
|
100
110
|
"node-call.then": "^1.0.0",
|
|
101
|
-
"
|
|
111
|
+
"noop-enterprise": "^2.0.1",
|
|
112
|
+
"noop10": "1.0.3",
|
|
102
113
|
"numero": "^0.1.1",
|
|
103
114
|
"object.entries-ponyfill": "^1.0.1",
|
|
104
115
|
"object.prototype-intrinsic-ai": "^1.0.1",
|
|
116
|
+
"positive-zero": "3.0.0",
|
|
117
|
+
"primitive-value-nan": "1.0.1",
|
|
118
|
+
"primitive-value-null": "1.0.0",
|
|
119
|
+
"primitive-value-undefined": "1.0.0",
|
|
105
120
|
"qc-core": "^0.0.0",
|
|
106
121
|
"repeating": "^2.0.1",
|
|
107
122
|
"simple-lru-cache": "^0.0.2",
|
|
123
|
+
"str-replaceallof-es": "1.0.0",
|
|
108
124
|
"string.prototype.at": "^1.0.6",
|
|
109
|
-
"string.prototype.replaceall": "
|
|
125
|
+
"string.prototype.replaceall": "1.0.11",
|
|
110
126
|
"string.prototype.split": "^1.0.9",
|
|
111
127
|
"subtract": "^0.0.3",
|
|
112
128
|
"true-value": "^3.0.0",
|