immediate-error 6.3.0 → 6.4.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
@@ -1,6 +1,13 @@
1
- # Immediate-error
1
+ # `immediate-error`
2
2
 
3
- This is a utility to throw an error.
3
+ This is a utility to throw an error in FP.
4
+
5
+ ## Installation
6
+ ```bash
7
+ $ npm install immediate-error
8
+ ```
9
+
10
+ ## Usage
4
11
 
5
12
  ```javascript
6
13
 
@@ -35,14 +42,5 @@ class MyCustomError extends Error {
35
42
  immediateError('Error!', MyCustomError) // throws a MyCustomError with the message "Error!" which in turn prints out "Custom: Error!" because we used our own error class
36
43
 
37
44
  ```
38
-
39
- ## Why?
40
-
41
- This module is great because it uses the best practices in JavaScript such as using dependencies like [`yanoop`](https://npmjs.com/package/yanoop) to throw errors and using [`es-errors`](https://npmjs.com/package/es-errors) instead of using the error classes natively.
42
-
43
- ## why?
44
- why not
45
-
46
- ## Show your support
47
-
48
- [Follow me on GitHub](https://github.com/tj-commits) and star my repositories.
45
+ ## License
46
+ Unlicense
package/index.d.ts CHANGED
@@ -9,4 +9,11 @@ export enum ErrorType {
9
9
  NativeAssertionError = 7
10
10
  }
11
11
 
12
- export function immediateError(message?: string, errorType?: ErrorType | Function): void
12
+ export type CustomError = {
13
+ new (message: string): never
14
+ }
15
+
16
+ export function immediateError(
17
+ message: string,
18
+ errorType?: ErrorType | CustomError
19
+ ): never
package/index.js CHANGED
@@ -1,99 +1,79 @@
1
- require('vanilla-javascript')
2
- require('none')()
3
-
4
- const GetIntrinsic = require('get-intrinsic')
5
- const zero = require('number-zero')
6
- const one = require('the-number-one').default
7
- const two = require('two')
8
- const three = require('numeric-constant-three')
9
- const four = require('always-four')
10
- const five = require('five')
11
- const six = require('number-six') // 6
12
- const seven = require('se7en') // 7
13
- const { throwop } = require('yanoop')
14
- const isError = require('is-error')
15
- const assert = require('assert-fn')
16
- const nativeAssert = require('node:assert')
17
- const vm = require('node:vm')
18
- const hasSelfEquality = require('has-self-equality')
19
-
20
- const $BaseError = require('es-errors')
1
+ const GetIntrinsic = require("get-intrinsic")
2
+ const $Object = require("es-object-atoms")
3
+ const zero = require("@positive-numbers/zero")
4
+ const one = require("@positive-numbers/one")
5
+ const two = require("@positive-numbers/two")
6
+ const three = require("@positive-numbers/three")
7
+ const four = require("@positive-numbers/four")
8
+ const five = require("@positive-numbers/five")
9
+ const six = require("@positive-numbers/six")
10
+ const seven = require("@positive-numbers/seven")
11
+ const { throwop, noop } = require("yanoop")
12
+ const { Switch } = require("switch-in-fp")
13
+ const assert = require("assert-fn")
14
+ const nativeAssert = require("node:assert")
15
+ const vm = require("node:vm")
16
+ const construct = require("construct-new")
17
+ const attempt = require("attempt-statement")
18
+
19
+ const $BaseError = require("es-errors")
21
20
  const $AssertionError = assert.AssertionError
22
- const $AggregateError = GetIntrinsic('%AggregateError%')
23
- const $RangeError = require('es-errors/range')
24
- const $ReferenceError = require('es-errors/ref')
25
- const $SyntaxError = require('es-errors/syntax')
26
- const $TypeError = require('es-errors/type')
21
+ const $AggregateError = GetIntrinsic("%AggregateError%")
22
+ const $RangeError = require("es-errors/range")
23
+ const $ReferenceError = require("es-errors/ref")
24
+ const $SyntaxError = require("es-errors/syntax")
25
+ const $TypeError = require("es-errors/type")
27
26
  const $NativeAssertionError = nativeAssert.AssertionError
28
27
 
29
- const default_error = 'ERROR!'
28
+ const default_error = "ERROR!"
30
29
 
31
- const ERROR = Object.freeze({
30
+ const ErrorType = $Object.freeze({
32
31
  BaseError: zero,
33
32
  AssertionError: one,
34
- AggregateError: two(),
35
- RangeError: three(),
36
- ReferenceError: four(),
37
- SyntaxError: five(),
38
- TypeError: six(),
39
- NativeAssertionError: seven()
33
+ AggregateError: two,
34
+ RangeError: three,
35
+ ReferenceError: four,
36
+ SyntaxError: five,
37
+ TypeError: six,
38
+ NativeAssertionError: seven
40
39
  })
41
40
 
42
- exports.immediateError = function immediateError(message = default_error, errorType = ERROR.Error) {
41
+ exports.immediateError = function immediateError(message = default_error, errorType = ErrorType.BaseError) {
43
42
  var error
44
43
 
45
- if (hasSelfEquality(isError)) {
46
- switch (errorType) {
47
- case ERROR.BaseError: {
48
- error = new $BaseError(message)
49
- break
50
- }
51
-
52
- case ERROR.AssertionError: {
53
- error = new $AssertionError(message)
54
- break
55
- }
56
-
57
- case ERROR.AggregateError: {
58
- error = new $AggregateError(message)
59
- break
60
- }
61
-
62
- case ERROR.RangeError: {
63
- error = new $RangeError(message)
64
- break
65
- }
66
-
67
- case ERROR.ReferenceError: {
68
- error = new $ReferenceError(message)
69
- break
70
- }
71
-
72
- case ERROR.SyntaxError: {
73
- error = new $SyntaxError(message)
74
- break
75
- }
76
-
77
- case ERROR.TypeError: {
78
- error = new $TypeError(message)
79
- break
80
- }
81
-
82
- case ERROR.NativeAssertionError: {
83
- error = new $NativeAssertionError(message)
84
- break
85
- }
86
-
87
- default: {
88
- try {
89
- error = new errorType(message)
90
- } catch (err) {
91
- [err] // put the error behind bars, where it belongs
92
- error = new $BaseError(message)
93
- }
94
- }
95
- }
96
- }
44
+ Switch(errorType)
45
+ .case(ErrorType.BaseError, function () {
46
+ error = construct({ target: $BaseError, args: [message] })
47
+ })
48
+ .case(ErrorType.AssertionError, function () {
49
+ error = construct({ target: $AssertionError, args: [message] })
50
+ })
51
+ .case(ErrorType.AggregateError, function () {
52
+ error = construct({ target: $AggregateError, args: [message] })
53
+ })
54
+ .case(ErrorType.RangeError, function () {
55
+ error = construct({ target: $RangeError, args: [message] })
56
+ })
57
+ .case(ErrorType.ReferenceError, function () {
58
+ error = construct({ target: $ReferenceError, args: [message] })
59
+ })
60
+ .case(ErrorType.SyntaxError, function () {
61
+ error = construct({ target: $SyntaxError, args: [message] })
62
+ })
63
+ .case(ErrorType.TypeError, function () {
64
+ error = construct({ target: $TypeError, args: [message] })
65
+ })
66
+ .case(ErrorType.NativeAssertionError, function () {
67
+ error = construct({ target: $NativeAssertionError, args: [message] })
68
+ })
69
+ .else(function () {
70
+ attempt(function() {
71
+ error = construct({ target: errorType, args: [message] })
72
+ }).rescue(function() {
73
+ error = construct({ target: $BaseError, args: [message] })
74
+ }).else(noop).ensure(noop).end()
75
+ })
76
+ .execute()
97
77
 
98
78
  const context = {
99
79
  error,
@@ -101,9 +81,9 @@ exports.immediateError = function immediateError(message = default_error, errorT
101
81
  }
102
82
  vm.createContext(context)
103
83
 
104
- const script = new vm.Script(`throwop(error)`, { filename: default_error })
84
+ const script = construct({ target: vm.Script, args: [`throwop(error)`, { filename: default_error }] })
105
85
 
106
86
  script.runInContext(context)
107
87
  }
108
88
 
109
- exports.ErrorType = ERROR
89
+ exports.ErrorType = ErrorType
package/index.test.js ADDED
@@ -0,0 +1,46 @@
1
+ const { immediateError, ErrorType } = require('immediate-error')
2
+ const assert = require('node:assert')
3
+
4
+ describe('immediateError utility', () => {
5
+
6
+ // Basic Usage
7
+ test('throws a regular Error with default message when no arguments are passed', () => {
8
+ expect(() => immediateError()).toThrow(Error)
9
+ expect(() => immediateError()).toThrow('ERROR!')
10
+ })
11
+
12
+ test('throws a regular Error with a custom message', () => {
13
+ expect(() => immediateError('Aaaaah')).toThrow(Error)
14
+ expect(() => immediateError('Aaaaah')).toThrow('Aaaaah')
15
+ })
16
+
17
+ // Native Error Types
18
+ test.each([
19
+ ['RangeError', ErrorType.RangeError, RangeError],
20
+ ['ReferenceError', ErrorType.ReferenceError, ReferenceError],
21
+ ['SyntaxError', ErrorType.SyntaxError, SyntaxError],
22
+ ['TypeError', ErrorType.TypeError, TypeError],
23
+ ['AggregateError', ErrorType.AggregateError, AggregateError],
24
+ ])('throws %s when specified', (name, type, constructor) => {
25
+ expect(() => immediateError('test message', type)).toThrow(constructor)
26
+ })
27
+
28
+ // Assertion Errors
29
+ test('throws NativeAssertionError (node:assert)', () => {
30
+ expect(() => immediateError('failed', ErrorType.NativeAssertionError))
31
+ .toThrow(assert.AssertionError)
32
+ })
33
+
34
+ // Custom Error Classes
35
+ test('throws a custom user-defined Error class', () => {
36
+ class MyCustomError extends Error {
37
+ constructor(message) {
38
+ super("Custom: " + message)
39
+ this.name = "MyCustomError"
40
+ }
41
+ }
42
+
43
+ expect(() => immediateError('Error!', MyCustomError)).toThrow(MyCustomError)
44
+ expect(() => immediateError('Error!', MyCustomError)).toThrow('Custom: Error!')
45
+ })
46
+ })
package/package.json CHANGED
@@ -1,43 +1,75 @@
1
1
  {
2
2
  "name": "immediate-error",
3
- "version": "6.3.0",
4
- "description": "Throw errors, better.",
3
+ "version": "6.4.0",
4
+ "description": "throw errors in fp",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
7
+ "test": "jest index.test.js"
8
8
  },
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "git+https://github.com/tj-commits/immediate-error.git"
11
+ "url": "git+https://github.com/in-fp/immediate-error.git"
12
12
  },
13
13
  "keywords": [
14
14
  "error",
15
15
  "throw",
16
16
  "immediate-error",
17
- "tool"
17
+ "tool",
18
+ "10x'ness",
19
+ "10x'ly made",
20
+ "10x",
21
+ "10x-engineer",
22
+ "10x engineer",
23
+ "10x engineering",
24
+ "jsfruit",
25
+ "libperson",
26
+ "libvegetable",
27
+ "bail",
28
+ "throw-error",
29
+ "fox",
30
+ "sigma",
31
+ "lifesaver",
32
+ "framework",
33
+ "js",
34
+ "ecmascript",
35
+ "javascript",
36
+ "licensed",
37
+ "unlicensed-licensed-license",
38
+ "jest",
39
+ "67",
40
+ "fp",
41
+ "in-fp",
42
+ "sigmaskibidi",
43
+ "tj-commits",
44
+ "skibidi-toilet-hacker",
45
+ "stevelib"
18
46
  ],
19
- "author": "tj-commits",
47
+ "author": "10x'ly Made",
20
48
  "license": "Unlicense",
21
49
  "bugs": {
22
- "url": "https://github.com/tj-commits/immediate-error/issues"
50
+ "url": "https://github.com/in-fp/immediate-error/issues"
23
51
  },
24
- "homepage": "https://github.com/tj-commits/immediate-error#readme",
52
+ "homepage": "https://github.com/in-fp/immediate-error#readme",
25
53
  "dependencies": {
26
- "always-four": "^1.0.0",
54
+ "@positive-numbers/five": "^3.0.0",
55
+ "@positive-numbers/four": "^3.0.0",
56
+ "@positive-numbers/one": "^3.0.0",
57
+ "@positive-numbers/seven": "^3.0.0",
58
+ "@positive-numbers/six": "^3.0.0",
59
+ "@positive-numbers/three": "^3.0.0",
60
+ "@positive-numbers/two": "^3.0.0",
61
+ "@positive-numbers/zero": "^3.0.0",
27
62
  "assert-fn": "^1.0.1",
63
+ "attempt-statement": "^1.2.0",
64
+ "construct-new": "^2.0.3",
28
65
  "es-errors": "^1.3.0",
29
- "five": "^0.8.0",
30
- "get-intrinsic": "^1.3.0",
31
- "has-self-equality": "^0.0.1",
32
- "is-error": "^2.2.2",
33
- "none": "^1.0.0",
34
- "number-six": "^1.0.1",
66
+ "es-object-atoms": "^1.1.1",
67
+ "get-intrinsic": "^1.3.1",
35
68
  "number-zero": "^1.0.3",
36
- "numeric-constant-three": "^1.0.0",
37
- "se7en": "^1.0.0",
38
- "the-number-one": "^1.0.1",
39
- "two": "^1.0.7",
40
- "vanilla-javascript": "^1.1.1",
69
+ "switch-in-fp": "^3.0.0",
41
70
  "yanoop": "^1.0.0"
71
+ },
72
+ "devDependencies": {
73
+ "jest": "^30.2.0"
42
74
  }
43
75
  }