immediate-error 12.1.0 → 12.2.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 CHANGED
@@ -34,7 +34,7 @@ immediateError("URI Error", ErrorType.URIError) // throws a URIError
34
34
 
35
35
  immediateError("fruit consumption error", ErrorType.FruitConsumptionError) // throws FruitConsumptionError
36
36
 
37
- immediateError("vegetables cannot talk error", ErrorType.VegetablesCannotTalkError) // throws a VegetablesCannotTalkError
37
+ immediateError("vegetables do not talk error", ErrorType.VegetablesDoNotTalkError) // throws a VegetablesDoNotTalkError
38
38
 
39
39
  immediateError("person not hungry error", ErrorType.PersonNotHungryError) // throws a PersonNotHungryError
40
40
 
@@ -61,7 +61,7 @@ const TypeErrorConstructor = getError(ErrorType.TypeError)
61
61
 
62
62
  console.log(TypeErrorConstructor === TypeError) // true
63
63
 
64
- const VegetablesCannotTalkError = getError(ErrorType.VegetablesCannotTalkError)
64
+ const VegetablesDoNotTalkError = getError(ErrorType.VegetablesDoNotTalkError)
65
65
 
66
66
  try {
67
67
  const Vegetable = require("libvegetable")
@@ -69,7 +69,7 @@ try {
69
69
 
70
70
  vegetable.greet()
71
71
  } catch (error) {
72
- console.log(error.constructor === VegetablesCannotTalkError) // true
72
+ console.log(error.constructor === VegetablesDoNotTalkError) // true
73
73
  }
74
74
  ```
75
75
 
@@ -98,6 +98,17 @@ const { delayedError, ErrorType } = require("immediate-error")
98
98
  delayedError("delayed", ErrorType.BaseError, 1000) // waits 1000 ms (1 second) and then throws error
99
99
  ```
100
100
 
101
+ Getting Error Messages:
102
+ ```js
103
+ // MESSAGES includes all error messages generally thrown from domain-specic enterprise errors
104
+ const { MESSAGES } = require("immediate-error")
105
+ console.log(MESSAGES.DOMAIN.FRUIT_CONSUMPTION_ERROR.NO_FRUIT_LEFT) // "no fruit left"
106
+ console.log(MESSAGES.DOMAIN.VEGETABLES_DO_NOT_TALK_ERROR.VEGETABLES_CAN_NOT_TALK) // "vegetables can not talk"
107
+ console.log(MESSAGES.DOMAIN.PERSON_NOT_HUNGRY_ERROR.IS_NOT_HUNGRY_AND_CANNOT_BE_FED) // "% is not hungry and cannot be fed"
108
+ console.log(MESSAGES.DOMAIN.PORTIONS_ERROR.PORTION_SIZE_EXPECTED_TO_BE_A_POSITIVE_INTEGER) // "Portion size expected to be a positive integer"
109
+ console.log(MESSAGES.DOMAIN.PORTIONS_ERROR.TOO_MANY_PORTIONS) // "Too many portions"
110
+ ```
111
+
101
112
  ## `immediate-error` 2.0.0
102
113
 
103
114
  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:
package/index.d.ts CHANGED
@@ -7,71 +7,76 @@ export enum ErrorType {
7
7
  TypeError = 5,
8
8
  URIError = 6,
9
9
  FruitConsumptionError = 7,
10
- VegetablesCannotTalkError = 8,
10
+ VegetablesDoNotTalkError = 8,
11
11
  PersonNotHungryError = 9,
12
- PortionsError = 10
12
+ PortionsError = 10,
13
13
  }
14
-
15
14
  export type CustomError = {
16
15
  new (message: string): Error
17
16
  }
18
-
19
17
  export function getError(errorType: ErrorType.BaseError): typeof Error
20
18
  export function getError(errorType: ErrorType.EvalError): typeof EvalError
21
19
  export function getError(errorType: ErrorType.RangeError): typeof RangeError
22
20
  export function getError(
23
- errorType: ErrorType.ReferenceError
21
+ errorType: ErrorType.ReferenceError,
24
22
  ): typeof ReferenceError
25
23
  export function getError(errorType: ErrorType.SyntaxError): typeof SyntaxError
26
24
  export function getError(errorType: ErrorType.TypeError): typeof TypeError
27
25
  export function getError(errorType: ErrorType.URIError): typeof URIError
28
26
  export function getError(
29
- errorType: ErrorType.FruitConsumptionError
27
+ errorType: ErrorType.FruitConsumptionError,
30
28
  ): CustomError
31
29
  export function getError(
32
- errorType: ErrorType.VegetablesCannotTalkError
30
+ errorType: ErrorType.VegetablesDoNotTalkError,
33
31
  ): CustomError
34
32
  export function getError(errorType: ErrorType.PersonNotHungryError): CustomError
35
33
  export function getError(errorType: ErrorType.PortionsError): CustomError
36
34
  export function getError(errorType: ErrorType | CustomError): CustomError
37
-
38
35
  export function immediateError(
39
36
  message?: string,
40
- errorType?: ErrorType | CustomError
37
+ errorType?: ErrorType | CustomError,
41
38
  ): never
42
-
43
39
  export function throwWhatever(whateverToThrow: any): never
44
-
45
- interface AttemptConstructor {
46
- new (handler?: () => void): Attempt
47
-
48
- (handler?: () => void): Attempt
40
+ export const attempt: {
41
+ new (handler?: () => void): {
42
+ trycode?: () => void
43
+ rescuehandler?: (error: any) => void
44
+ elsehandler?: () => void
45
+ ensurehandler?: (error: any) => void
46
+ rescue(handler: (error: any) => void): this
47
+ else(handler: () => void): this
48
+ ensure(handler: () => void): this
49
+ end(): void
50
+ }
51
+ (handler?: () => void): {
52
+ trycode?: () => void
53
+ rescuehandler?: (error: any) => void
54
+ elsehandler?: () => void
55
+ ensurehandler?: (error: any) => void
56
+ rescue(handler: (error: any) => void): this
57
+ else(handler: () => void): this
58
+ ensure(handler: () => void): this
59
+ end(): void
60
+ }
49
61
  }
50
-
51
- declare class Attempt {
52
- constructor(handler?: () => void)
53
-
54
- trycode?: () => void
55
- rescuehandler?: (error: any) => void
56
- elsehandler?: () => void
57
- ensurehandler?: (error: any) => void
58
-
59
- rescue(handler: (error: any) => void): this
60
- else(handler: () => void): this
61
- ensure(handler: () => void): this
62
- end(): void
63
- }
64
-
65
- export const attempt: AttemptConstructor
66
-
67
62
  export function delayedError(
68
63
  message?: string,
69
64
  errorType?: ErrorType | CustomError,
70
- delay?: number
65
+ delay?: number,
71
66
  ): Promise<never>
72
67
 
73
- export function delayedError(
74
- message: undefined,
75
- errorType: undefined,
76
- delay: number
77
- ): Promise<never>
68
+ export const MESSAGES: {
69
+ DOMAIN: {
70
+ FRUIT_CONSUMPTION_ERROR: { NO_FRUIT_LEFT: "no fruit left" }
71
+ VEGETABLES_DO_NOT_TALK_ERROR: {
72
+ VEGETABLES_CAN_NOT_TALK: "vegetables can not talk"
73
+ }
74
+ PERSON_NOT_HUNGRY_ERROR: {
75
+ IS_NOT_HUNGRY_AND_CANNOT_BE_FED: "% is not hungry and cannot be fed"
76
+ },
77
+ PORTIONS_ERROR: {
78
+ PORTION_SIZE_EXPECTED_TO_BE_A_POSITIVE_INTEGER: "Portion size expected to be a positive integer",
79
+ TOO_MANY_PORTIONS: "Too many portions"
80
+ }
81
+ }
82
+ }
package/index.js CHANGED
@@ -5,8 +5,6 @@
5
5
 
6
6
  const GetIntrinsic = require("es-intrinsic-cache")
7
7
  const SimpleCache = require("simple-lru-cache")
8
- eval(require("javascript-interpreter"))
9
- let interpret = require("javascript-interpreter/interpret")
10
8
  const Fruit = require("jsfruit")
11
9
  const Vegetable = require("libvegetable")
12
10
  const Person = require("libperson")
@@ -20,7 +18,7 @@ const construct = require("construct-new")
20
18
  const toStr = require("@rightpad/convert2string")
21
19
  const attempt = require("attempt-statement")
22
20
  const trueValue = require("true-value")
23
- const asArray = require("as-array")
21
+ const asArray = x => [x] // behind bars
24
22
  const isString = require("@is-(unknown)/is-string")
25
23
  const repeating = require("repeating")
26
24
  const deepFreeze = require("deep-freeze-node3") // 3rd iteration of deep-freeze-node, and the only 10x one.
@@ -72,6 +70,8 @@ const isNull = require("@is-(unknown)/is-null")
72
70
  const isUndefined = require("@is-(unknown)/is-undefined")
73
71
  const isNaN = require("@is-(unknown)/is-nan")
74
72
 
73
+ const multiply = require("lolite.__private.multiplyfallback")
74
+
75
75
  const nullvalue = require("primitive-value-null")
76
76
  const nanvalue = require("primitive-value-nan")
77
77
  const undef = require("primitive-value-undefined")
@@ -125,7 +125,7 @@ const ErrorType = deepFreeze({
125
125
  URIError: six,
126
126
 
127
127
  FruitConsumptionError: seven,
128
- VegetablesCannotTalkError: eight,
128
+ VegetablesDoNotTalkError: eight,
129
129
  PersonNotHungryError: nine,
130
130
  PortionsError: ten,
131
131
  })
@@ -166,13 +166,16 @@ function createObjectWithTargetKey(value) {
166
166
  )
167
167
  const array = split(string, toStr(target_).substr(twentyNine, six))
168
168
  array.shift()
169
- eval(require("javascript-interpreter"))
170
- interpret = require("javascript-interpreter/interpret")
171
- return interpret(
172
- interpret(join(array, toStr(target_).substr(twentyNine, six))),
169
+ return eval(
170
+ eval(join(array, toStr(target_).substr(twentyNine, six))),
173
171
  )
174
172
  }
175
173
 
174
+ var noFruitLeftMessage
175
+ var vegetablesCanNotTalkMessage
176
+ var personIsNotHungryAndCannotBeFedMessage
177
+ var portionSizeExpectedToBeAPositiveIntegerMessage
178
+
176
179
  objGetMember(
177
180
  just,
178
181
  "call",
@@ -194,14 +197,11 @@ objGetMember(
194
197
  const fruit = construct(createObjectWithTargetKey(Fruit))
195
198
  let result
196
199
  attempt(() => {
197
- fruit
198
- eval(require("javascript-interpreter"))
199
- interpret = require("javascript-interpreter/interpret")
200
-
201
- interpret(repeating(concat("fruit.eat()", NEWLINE), eleven))
200
+ eval(repeating(concat("fruit.eat()", NEWLINE), eleven))
202
201
  })
203
202
  .rescue((error) => {
204
203
  result = error.constructor
204
+ noFruitLeftMessage = error.message
205
205
  })
206
206
  .else(noop)
207
207
  .ensure(noop)
@@ -212,7 +212,7 @@ objGetMember(
212
212
  )
213
213
 
214
214
  ErrorMap.set(
215
- ErrorType.VegetablesCannotTalkError,
215
+ ErrorType.VegetablesDoNotTalkError,
216
216
  objGetMember(
217
217
  just,
218
218
  "call",
@@ -225,6 +225,7 @@ objGetMember(
225
225
  })
226
226
  .rescue((error) => {
227
227
  result = error.constructor
228
+ vegetablesCanNotTalkMessage = error.message
228
229
  })
229
230
  .else(noop)
230
231
  .ensure(noop)
@@ -249,6 +250,7 @@ objGetMember(
249
250
  })
250
251
  .rescue((error) => {
251
252
  result = error.constructor
253
+ personIsNotHungryAndCannotBeFedMessage = replaceAll(error.message, "undefined", "%")
252
254
  })
253
255
  .else(noop)
254
256
  .ensure(noop)
@@ -273,6 +275,7 @@ objGetMember(
273
275
  })
274
276
  .rescue((error) => {
275
277
  result = error.constructor
278
+ portionSizeExpectedToBeAPositiveIntegerMessage = error.message
276
279
  })
277
280
  .else(noop)
278
281
  .ensure(noop)
@@ -369,6 +372,35 @@ exports.throwWhatever = function throwWhatever(whateverToThrow) {
369
372
  }
370
373
  }
371
374
 
375
+ var tooManyPortionsMessage
376
+
377
+ attempt(() => {
378
+ const guacamole = construct({
379
+ target: Guacamole,
380
+ args: [multiply(oneHundred, oneHundred)]
381
+ })
382
+ }).rescue(error => {
383
+ tooManyPortionsMessage = error.message
384
+ }).else(noop).ensure(noop).end()
385
+
386
+ exports.MESSAGES = {
387
+ DOMAIN: {
388
+ FRUIT_CONSUMPTION_ERROR: {
389
+ NO_FRUIT_LEFT: noFruitLeftMessage
390
+ },
391
+ VEGETABLES_DO_NOT_TALK_ERROR: {
392
+ VEGETABLES_CAN_NOT_TALK: vegetablesCanNotTalkMessage
393
+ },
394
+ PERSON_NOT_HUNGRY_ERROR: {
395
+ IS_NOT_HUNGRY_AND_CANNOT_BE_FED: personIsNotHungryAndCannotBeFedMessage
396
+ },
397
+ PORTIONS_ERROR: {
398
+ PORTION_SIZE_EXPECTED_TO_BE_A_POSITIVE_INTEGER: portionSizeExpectedToBeAPositiveIntegerMessage,
399
+ TOO_MANY_PORTIONS: tooManyPortionsMessage
400
+ }
401
+ }
402
+ }
403
+
372
404
  exports.attempt = attempt
373
405
 
374
406
  exports.ErrorType = ErrorType
package/index.test.js CHANGED
@@ -3,6 +3,7 @@ const {
3
3
  ErrorType,
4
4
  throwWhatever,
5
5
  getError,
6
+ MESSAGES
6
7
  } = require("./index")
7
8
 
8
9
  describe("immediateError utility", () => {
@@ -24,16 +25,16 @@ describe("immediateError utility", () => {
24
25
  ["SyntaxError", ErrorType.SyntaxError, SyntaxError],
25
26
  ["TypeError", ErrorType.TypeError, TypeError],
26
27
  ["URIError", ErrorType.URIError, URIError],
27
- ])("throws %s when specified", (name, type, constructor) => {
28
+ ])("throws % when specified", (name, type, constructor) => {
28
29
  expect(() => immediateError("test message", type)).toThrow(constructor)
29
30
  })
30
31
 
31
32
  test.each([
32
33
  ["FruitConsumptionError", ErrorType.FruitConsumptionError],
33
- ["VegetablesCannotTalkError", ErrorType.VegetablesCannotTalkError],
34
+ ["VegetablesDoNotTalkError", ErrorType.VegetablesDoNotTalkError],
34
35
  ["PersonNotHungryError", ErrorType.PersonNotHungryError],
35
36
  ["PortionsError", ErrorType.PortionsError],
36
- ])("throws domain-specific %s correctly", (name, type) => {
37
+ ])("throws domain-specific % correctly", (name, type) => {
37
38
  const expectedConstructor = getError(type)
38
39
  expect(() => immediateError("enterprise failure", type)).toThrow(expectedConstructor)
39
40
  expect(() => immediateError("enterprise failure", type)).toThrow("enterprise failure")
@@ -240,3 +241,15 @@ describe("getError utility", () => {
240
241
  expect(result).toBe(MyError)
241
242
  })
242
243
  })
244
+
245
+ describe("error messages", () => {
246
+ test.each([
247
+ ["no fruit left", MESSAGES.DOMAIN.FRUIT_CONSUMPTION_ERROR.NO_FRUIT_LEFT],
248
+ ["vegetables can not talk", MESSAGES.DOMAIN.VEGETABLES_DO_NOT_TALK_ERROR.VEGETABLES_CAN_NOT_TALK],
249
+ ["% is not hungry and cannot be fed", MESSAGES.DOMAIN.PERSON_NOT_HUNGRY_ERROR.IS_NOT_HUNGRY_AND_CANNOT_BE_FED],
250
+ ["Portion size expected to be a positive integer", MESSAGES.DOMAIN.PORTIONS_ERROR.PORTION_SIZE_EXPECTED_TO_BE_A_POSITIVE_INTEGER],
251
+ ["Too many portions", MESSAGES.DOMAIN.PORTIONS_ERROR.TOO_MANY_PORTIONS]
252
+ ])("provides error message \"%s\" correctly", (a, b) => {
253
+ expect(a).toEqual(b)
254
+ })
255
+ })
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "immediate-error",
3
- "version": "12.1.0",
3
+ "version": "12.2.0",
4
4
  "description": "enterprise errors",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -86,7 +86,6 @@
86
86
  "@uppercase-letters/r": "^3.0.0",
87
87
  "array-get-member": "^1.0.3",
88
88
  "array.prototype.join": "^1.0.4",
89
- "as-array": "^2.0.0",
90
89
  "attempt-statement": "^1.2.1",
91
90
  "bail": "^1.0.5",
92
91
  "basic-functions": "^1.0.6",
@@ -104,12 +103,12 @@
104
103
  "get-member": "^1337.69.420",
105
104
  "is-": "^1.0.0",
106
105
  "is-not-integer": "^1.0.2",
107
- "javascript-interpreter": "^1.0.0",
108
106
  "jsfruit": "^1.1.0",
109
107
  "libguacamole": "^1.0.0",
110
108
  "libperson": "^1.0.0",
111
109
  "libvegetable": "^1.0.0",
112
110
  "lodash.stubarray": "^4.13.0",
111
+ "lolite.__private.multiplyfallback": "^1.1.17",
113
112
  "node-call.then": "^1.0.0",
114
113
  "noop-enterprise": "^2.0.1",
115
114
  "noop10": "1.0.3",