immediate-error 6.2.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,17 +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 [`throw-error`](https://npmjs.com/package/throw-error) and [`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
- ## Show your support
44
-
45
- [Follow me on GitHub](https://github.com/tj-commits) and star my repositories.
46
-
47
- ## is this a joke?
48
-
49
- Of course not!
50
- <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
51
- (Yes, it is.)
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,120 +1,89 @@
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')
12
- const seven = require('se7en')
13
- const { throwop } = require('yanoop')
14
- const throwError = require('throw-error')
15
- const If = require('if')
16
- const isError = require('is-error')
17
- const assert = require('assert-fn')
18
- const nativeAssert = require('node:assert')
19
- const vm = require('node:vm')
20
- const hasSelfEquality = require('has-self-equality')
21
- const noop = require('noop10')
22
-
23
- const $BaseError = require('es-errors')
24
- const $AssertionError = assert.AssertionError
25
- const $AggregateError = GetIntrinsic('%AggregateError%')
26
- const $RangeError = require('es-errors/range')
27
- const $ReferenceError = require('es-errors/ref')
28
- const $SyntaxError = require('es-errors/syntax')
29
- const $TypeError = require('es-errors/type')
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")
20
+ const $AssertionError = assert.AssertionError
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")
30
26
  const $NativeAssertionError = nativeAssert.AssertionError
31
27
 
32
- const MathRandom = GetIntrinsic('%Math.random%')
33
-
34
- const ERROR = Object.freeze({
35
- BaseError:zero,
36
- AssertionError: one,
37
- AggregateError: two(),
38
- RangeError: three(),
39
- ReferenceError: four(),
40
- SyntaxError: five(),
41
- TypeError: six(),
42
- NativeAssertionError: seven()
28
+ const default_error = "ERROR!"
29
+
30
+ const ErrorType = $Object.freeze({
31
+ BaseError: zero,
32
+ AssertionError: one,
33
+ AggregateError: two,
34
+ RangeError: three,
35
+ ReferenceError: four,
36
+ SyntaxError: five,
37
+ TypeError: six,
38
+ NativeAssertionError: seven
43
39
  })
44
40
 
45
- exports.immediateError = function immediateError(message = 'ERROR!', errorType = ERROR.Error) {
46
- var error
47
-
48
- if (hasSelfEquality(isError)) {
49
- switch (errorType) {
50
- case ERROR.BaseError: {
51
- error = new $BaseError(message)
52
- break
53
- }
54
-
55
- case ERROR.AssertionError: {
56
- error = new $AssertionError(message)
57
- break
58
- }
59
-
60
- case ERROR.AggregateError: {
61
- error = new $AggregateError(message)
62
- break
63
- }
64
-
65
- case ERROR.RangeError: {
66
- error = new $RangeError(message)
67
- break
68
- }
69
-
70
- case ERROR.ReferenceError: {
71
- error = new $ReferenceError(message)
72
- break
73
- }
74
-
75
- case ERROR.SyntaxError: {
76
- error = new $SyntaxError(message)
77
- break
78
- }
79
-
80
- case ERROR.TypeError: {
81
- error = new $TypeError(message)
82
- break
83
- }
84
-
85
- case ERROR.NativeAssertionError: {
86
- error = new $NativeAssertionError(message)
87
- break
88
- }
89
-
90
- default: {
91
- try {
92
- error = new errorType(message)
93
- } catch(err) {
94
- [err] // put the error behind bars, where it belongs
95
- error = new $BaseError(message)
96
- }
97
- }
98
- }
99
- }
100
-
101
- const context = {
102
- error,
103
- throwError,
104
- throwop,
105
- rand: MathRandom(),
106
- If
107
- }
108
- vm.createContext(context)
109
-
110
- const script = new vm.Script(`
111
- If(rand < 0.5).Then(() => {
112
- throwError(error)
113
- }).Else(() => {
114
- throwop(error)
115
- })`, { filename: `ERROR!`})
116
-
117
- script.runInContext(context)
41
+ exports.immediateError = function immediateError(message = default_error, errorType = ErrorType.BaseError) {
42
+ var error
43
+
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()
77
+
78
+ const context = {
79
+ error,
80
+ throwop
81
+ }
82
+ vm.createContext(context)
83
+
84
+ const script = construct({ target: vm.Script, args: [`throwop(error)`, { filename: default_error }] })
85
+
86
+ script.runInContext(context)
118
87
  }
119
88
 
120
- 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,46 +1,75 @@
1
- {
2
- "name": "immediate-error",
3
- "version": "6.2.0",
4
- "description": "Throw errors, better.",
5
- "main": "index.js",
6
- "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
8
- },
9
- "repository": {
10
- "type": "git",
11
- "url": "git+https://github.com/tj-commits/immediate-error.git"
12
- },
13
- "keywords": [
14
- "error",
15
- "throw",
16
- "immediate-error",
17
- "tool"
18
- ],
19
- "author": "tj-commits",
20
- "license": "UNLICENSED",
21
- "bugs": {
22
- "url": "https://github.com/tj-commits/immediate-error/issues"
23
- },
24
- "homepage": "https://github.com/tj-commits/immediate-error#readme",
25
- "dependencies": {
26
- "always-four": "^1.0.0",
27
- "assert-fn": "^1.0.1",
28
- "es-errors": "^1.3.0",
29
- "five": "^0.8.0",
30
- "get-intrinsic": "^1.3.0",
31
- "has-self-equality": "^0.0.1",
32
- "if": "^2.0.0",
33
- "is-error": "^2.2.2",
34
- "none": "^1.0.0",
35
- "noop10": "^1.0.3",
36
- "number-six": "^1.0.1",
37
- "number-zero": "^1.0.3",
38
- "numeric-constant-three": "^1.0.0",
39
- "se7en": "^1.0.0",
40
- "the-number-one": "^1.0.1",
41
- "throw-error": "^1.0.0",
42
- "two": "^1.0.7",
43
- "vanilla-javascript": "^1.1.1",
44
- "yanoop": "^1.0.0"
45
- }
46
- }
1
+ {
2
+ "name": "immediate-error",
3
+ "version": "6.4.0",
4
+ "description": "throw errors in fp",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "jest index.test.js"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/in-fp/immediate-error.git"
12
+ },
13
+ "keywords": [
14
+ "error",
15
+ "throw",
16
+ "immediate-error",
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"
46
+ ],
47
+ "author": "10x'ly Made",
48
+ "license": "Unlicense",
49
+ "bugs": {
50
+ "url": "https://github.com/in-fp/immediate-error/issues"
51
+ },
52
+ "homepage": "https://github.com/in-fp/immediate-error#readme",
53
+ "dependencies": {
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",
62
+ "assert-fn": "^1.0.1",
63
+ "attempt-statement": "^1.2.0",
64
+ "construct-new": "^2.0.3",
65
+ "es-errors": "^1.3.0",
66
+ "es-object-atoms": "^1.1.1",
67
+ "get-intrinsic": "^1.3.1",
68
+ "number-zero": "^1.0.3",
69
+ "switch-in-fp": "^3.0.0",
70
+ "yanoop": "^1.0.0"
71
+ },
72
+ "devDependencies": {
73
+ "jest": "^30.2.0"
74
+ }
75
+ }