immediate-error 6.3.0 → 7.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 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
 
@@ -12,12 +19,6 @@ immediateError('Aaaaah') // this will throw a regular Error with the message "Aa
12
19
 
13
20
  immediateError('Aaaaah', ErrorType.BaseError) // does the same thing as above
14
21
 
15
- immediateError('Aggregate error', ErrorType.AggregateError) // throws an AggregateError
16
-
17
- immediateError('Assertion error', ErrorType.AssertionError) // throws an AssertionError (from the assert-fn module)
18
-
19
- immediateError('Assertion error', ErrorType.NativeAssertionError) // throws an AssertionError (from the node:assert module)
20
-
21
22
  immediateError('Range error', ErrorType.RangeError) // throws a RangeError
22
23
 
23
24
  immediateError('Reference error', ErrorType.ReferenceError) // throws a ReferenceError
@@ -35,14 +36,5 @@ class MyCustomError extends Error {
35
36
  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
37
 
37
38
  ```
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.
39
+ ## License
40
+ Unlicense
package/index.d.ts CHANGED
@@ -1,12 +1,18 @@
1
1
  export enum ErrorType {
2
2
  BaseError = 0,
3
- AggregateError = 1,
4
- AssertionError = 2,
5
- RangeError = 3,
6
- ReferenceError = 4,
7
- SyntaxError = 5,
8
- TypeError = 6,
9
- NativeAssertionError = 7
3
+ EvalError = 1,
4
+ RangeError = 2,
5
+ ReferenceError = 3,
6
+ SyntaxError = 4,
7
+ TypeError = 5,
8
+ URIError = 6
10
9
  }
11
10
 
12
- export function immediateError(message?: string, errorType?: ErrorType | Function): void
11
+ export type CustomError = {
12
+ new (message: string): never
13
+ }
14
+
15
+ export function immediateError(
16
+ message: string,
17
+ errorType?: ErrorType | CustomError
18
+ ): never
package/index.js CHANGED
@@ -1,109 +1,90 @@
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')
21
- 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')
27
- const $NativeAssertionError = nativeAssert.AssertionError
28
-
29
- const default_error = 'ERROR!'
30
-
31
- const ERROR = Object.freeze({
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 noop = require("yanoop").noop
11
+ const bail = require("bail")
12
+ const { Switch } = require("switch-in-fp")
13
+ const vm = require("node:vm")
14
+ const construct = require("construct-new")
15
+ const attempt = require("attempt-statement")
16
+ const trueValue = require("true-value")
17
+
18
+ const $BaseError = require("es-error-intrinsics/Error")
19
+ const $EvalError = require("es-error-intrinsics/EvalError")
20
+ const $RangeError = require("es-error-intrinsics/RangeError")
21
+ const $ReferenceError = require("es-error-intrinsics/ReferenceError")
22
+ const $SyntaxError = require("es-error-intrinsics/SyntaxError")
23
+ const $TypeError = require("es-error-intrinsics/TypeError")
24
+ const $URIError = require("es-error-intrinsics/URIError")
25
+
26
+ const captureStackTrace = GetIntrinsic("%Error.captureStackTrace%", trueValue())
27
+
28
+ const default_error = "ERROR!"
29
+
30
+ const ErrorType = $Object.freeze({
32
31
  BaseError: zero,
33
- AssertionError: one,
34
- AggregateError: two(),
35
- RangeError: three(),
36
- ReferenceError: four(),
37
- SyntaxError: five(),
38
- TypeError: six(),
39
- NativeAssertionError: seven()
32
+ EvalError: one,
33
+ RangeError: two,
34
+ ReferenceError: three,
35
+ SyntaxError: four,
36
+ TypeError: five,
37
+ URIError: six,
40
38
  })
41
39
 
42
- exports.immediateError = function immediateError(message = default_error, errorType = ERROR.Error) {
40
+ exports.immediateError = function immediateError(message = default_error, errorType = ErrorType.BaseError) {
43
41
  var error
44
42
 
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
- }
43
+ Switch(errorType)
44
+ .case(ErrorType.BaseError, function () {
45
+ error = construct({ target: $BaseError, args: [message] })
46
+ })
47
+ .case(ErrorType.EvalError, function () {
48
+ error = construct({ target: $EvalError, args: [message] })
49
+ })
50
+ .case(ErrorType.RangeError, function () {
51
+ error = construct({ target: $RangeError, args: [message] })
52
+ })
53
+ .case(ErrorType.ReferenceError, function () {
54
+ error = construct({ target: $ReferenceError, args: [message] })
55
+ })
56
+ .case(ErrorType.SyntaxError, function () {
57
+ error = construct({ target: $SyntaxError, args: [message] })
58
+ })
59
+ .case(ErrorType.TypeError, function () {
60
+ error = construct({ target: $TypeError, args: [message] })
61
+ })
62
+ .case(ErrorType.URIError, function () {
63
+ error = construct({ target: $URIError, args: [message] })
64
+ })
65
+ .else(function () {
66
+ attempt(function () {
67
+ error = construct({ target: errorType, args: [message] })
68
+ }).rescue(function () {
69
+ error = construct({ target: $BaseError, args: [message] })
70
+ }).else(noop).ensure(noop).end()
71
+ })
72
+ .execute()
73
+
74
+ if (captureStackTrace) {
75
+ captureStackTrace(error, immediateError)
96
76
  }
97
77
 
98
78
  const context = {
99
- error,
100
- throwop
79
+ error: error,
80
+ bail: bail
101
81
  }
82
+
102
83
  vm.createContext(context)
103
84
 
104
- const script = new vm.Script(`throwop(error)`, { filename: default_error })
85
+ const script = construct({ target: vm.Script, args: [`bail(error)`, { filename: default_error }] })
105
86
 
106
87
  script.runInContext(context)
107
88
  }
108
89
 
109
- exports.ErrorType = ERROR
90
+ exports.ErrorType = ErrorType
package/index.test.js ADDED
@@ -0,0 +1,41 @@
1
+ const { immediateError, ErrorType } = require('./index')
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
+ ['BaseError', ErrorType.BaseError, Error],
20
+ ['EvalError', ErrorType.EvalError, EvalError],
21
+ ['RangeError', ErrorType.RangeError, RangeError],
22
+ ['ReferenceError', ErrorType.ReferenceError, ReferenceError],
23
+ ['SyntaxError', ErrorType.SyntaxError, SyntaxError],
24
+ ['TypeError', ErrorType.TypeError, TypeError],
25
+ ['RangeError', ErrorType.URIError, URIError],
26
+ ])('throws %s when specified', (name, type, constructor) => {
27
+ expect(() => immediateError('test message', type)).toThrow(constructor)
28
+ })
29
+ // Custom Error Classes
30
+ test('throws a custom user-defined Error class', () => {
31
+ class MyCustomError extends Error {
32
+ constructor(message) {
33
+ super("Custom: " + message)
34
+ this.name = "MyCustomError"
35
+ }
36
+ }
37
+
38
+ expect(() => immediateError('Error!', MyCustomError)).toThrow(MyCustomError)
39
+ expect(() => immediateError('Error!', MyCustomError)).toThrow('Custom: Error!')
40
+ })
41
+ })
package/package.json CHANGED
@@ -1,43 +1,73 @@
1
1
  {
2
2
  "name": "immediate-error",
3
- "version": "6.3.0",
4
- "description": "Throw errors, better.",
3
+ "version": "7.0.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
+ "sigma",
30
+ "lifesaver",
31
+ "framework",
32
+ "js",
33
+ "ecmascript",
34
+ "javascript",
35
+ "licensed",
36
+ "jest",
37
+ "67",
38
+ "fp",
39
+ "in-fp",
40
+ "sigmaskibidi",
41
+ "tj-commits",
42
+ "skibidi-toilet-hacker",
43
+ "stevelib"
18
44
  ],
19
- "author": "tj-commits",
45
+ "author": "10x'ly Made",
20
46
  "license": "Unlicense",
21
47
  "bugs": {
22
- "url": "https://github.com/tj-commits/immediate-error/issues"
48
+ "url": "https://github.com/in-fp/immediate-error/issues"
23
49
  },
24
- "homepage": "https://github.com/tj-commits/immediate-error#readme",
50
+ "homepage": "https://github.com/in-fp/immediate-error#readme",
25
51
  "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
- "is-error": "^2.2.2",
33
- "none": "^1.0.0",
34
- "number-six": "^1.0.1",
52
+ "@positive-numbers/five": "^3.0.0",
53
+ "@positive-numbers/four": "^3.0.0",
54
+ "@positive-numbers/one": "^3.0.0",
55
+ "@positive-numbers/six": "^3.0.0",
56
+ "@positive-numbers/three": "^3.0.0",
57
+ "@positive-numbers/two": "^3.0.0",
58
+ "@positive-numbers/zero": "^3.0.0",
59
+ "attempt-statement": "^1.2.0",
60
+ "bail": "^1.0.5",
61
+ "construct-new": "^2.0.3",
62
+ "es-error-intrinsics": "^1.0.1",
63
+ "es-object-atoms": "^1.1.1",
64
+ "get-intrinsic": "^1.3.1",
35
65
  "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",
66
+ "switch-in-fp": "^3.0.0",
67
+ "true-value": "^2.0.5",
41
68
  "yanoop": "^1.0.0"
69
+ },
70
+ "devDependencies": {
71
+ "jest": "^30.2.0"
42
72
  }
43
73
  }