immediate-error 3.1.2 → 4.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 +34 -10
- package/index.d.ts +11 -0
- package/index.js +84 -61
- package/package.json +6 -4
package/README.md
CHANGED
|
@@ -4,22 +4,46 @@ This is a utility to throw an error.
|
|
|
4
4
|
|
|
5
5
|
```javascript
|
|
6
6
|
|
|
7
|
-
const immediateError = require('immediate-error')
|
|
7
|
+
const { immediateError, ErrorType } = require('immediate-error')
|
|
8
8
|
|
|
9
|
-
|
|
9
|
+
immediateError() // this will throw a regular Error with the default message of "ERROR!"
|
|
10
10
|
|
|
11
|
-
immediateError('Aaaaah') // this will throw
|
|
11
|
+
immediateError('Aaaaah') // this will throw a regular Error with the message "Aaaaah"
|
|
12
12
|
|
|
13
|
-
immediateError('Aaaaah',
|
|
13
|
+
immediateError('Aaaaah', ErrorType.BaseError) // does the same thing as above
|
|
14
14
|
|
|
15
|
-
immediateError('Assertion error',
|
|
15
|
+
immediateError('Assertion error', ErrorType.AssertionError) // throws an AssertionError (from the assert-fn module)
|
|
16
16
|
|
|
17
|
-
immediateError('
|
|
17
|
+
immediateError('Assertion error', ErrorType.NativeAssertionError) // throws an AssertionError (from the node:assert module)
|
|
18
18
|
|
|
19
|
-
immediateError('
|
|
19
|
+
immediateError('Range error', ErrorType.RangeError) // throws a RangeError
|
|
20
20
|
|
|
21
|
-
immediateError('
|
|
21
|
+
immediateError('Reference error', ErrorType.ReferenceError) // throws a ReferenceError
|
|
22
22
|
|
|
23
|
-
immediateError('
|
|
23
|
+
immediateError('Syntax error', ErrorType.SyntaxError) // throws a SyntaxError
|
|
24
24
|
|
|
25
|
-
|
|
25
|
+
immediateError('type error', ErrorType.TypeError) // throws a TypeError
|
|
26
|
+
|
|
27
|
+
class MyCustomError extends Error {
|
|
28
|
+
constructor (message) {
|
|
29
|
+
super("Custom: " + message)
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
immediateError('Error!', MyCustomError) // throws a MyCustomError with the message "Error!" which in turn prints out "Custom: Error!" because we used our own error class
|
|
34
|
+
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Why?
|
|
38
|
+
|
|
39
|
+
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.
|
|
40
|
+
|
|
41
|
+
## Show your support
|
|
42
|
+
|
|
43
|
+
[Follow me on GitHub](https://github.com/tj-commits) and star my repositories.
|
|
44
|
+
|
|
45
|
+
## is this a joke?
|
|
46
|
+
|
|
47
|
+
Of course not!
|
|
48
|
+
<br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br>
|
|
49
|
+
(Yes, it is.)
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export enum ErrorType {
|
|
2
|
+
BaseError = 0,
|
|
3
|
+
AssertionError = 1,
|
|
4
|
+
RangeError = 2,
|
|
5
|
+
ReferenceError = 3,
|
|
6
|
+
SyntaxError = 4,
|
|
7
|
+
TypeError = 5,
|
|
8
|
+
NativeAssertionError = 6
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function immediateError(message?: string, errorType?: ErrorType | Function): void
|
package/index.js
CHANGED
|
@@ -2,72 +2,92 @@ require('vanilla-javascript')
|
|
|
2
2
|
require('vapor-js-npm')
|
|
3
3
|
require('none')()
|
|
4
4
|
|
|
5
|
-
const one = require('the-number-one').default
|
|
6
|
-
const two = require('two')
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
require('
|
|
11
|
-
const
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
const If = require('if') // conditional chaining for javascript
|
|
5
|
+
const one = require('the-number-one').default
|
|
6
|
+
const two = require('two')
|
|
7
|
+
const three = require('numeric-constant-three')
|
|
8
|
+
const four = require('always-four')
|
|
9
|
+
const five = require('five')
|
|
10
|
+
const six = require('number-six')
|
|
11
|
+
const { throwop } = require('yanoop')
|
|
12
|
+
const throwError = require('throw-error')
|
|
13
|
+
const If = require('if')
|
|
15
14
|
const clc = require('cli-color')
|
|
15
|
+
const isError = require('is-error')
|
|
16
|
+
const assert = require('assert-fn')
|
|
17
|
+
const nativeAssert = require('node:assert')
|
|
18
|
+
const vm = require('node:vm')
|
|
19
|
+
|
|
20
|
+
const $BaseError = require('es-errors')
|
|
21
|
+
const $AssertionError = assert.AssertionError
|
|
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')
|
|
26
|
+
const $NativeAssertionError = nativeAssert.AssertionError
|
|
16
27
|
|
|
17
28
|
const ERROR = Object.freeze({
|
|
18
|
-
|
|
29
|
+
BaseError:0,
|
|
19
30
|
AssertionError: one,
|
|
20
31
|
RangeError: two(),
|
|
21
|
-
ReferenceError:
|
|
22
|
-
SyntaxError:
|
|
23
|
-
TypeError: five()
|
|
32
|
+
ReferenceError: three(),
|
|
33
|
+
SyntaxError: four(),
|
|
34
|
+
TypeError: five(),
|
|
35
|
+
NativeAssertionError: six()
|
|
24
36
|
})
|
|
25
37
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
module.exports = function immediateError(message = 'YOU SUCK AT PROGRAMMING THERE WAS A FUCKING ERROR!', options = {
|
|
30
|
-
errorType: ERROR.Error
|
|
31
|
-
}) {
|
|
32
|
-
var error = new Error(message) /* create an error
|
|
33
|
-
variable and then use switch statement to set it*/
|
|
34
|
-
switch(options.errorType) {
|
|
35
|
-
case ERROR.Error: {
|
|
36
|
-
error = new Error(message)
|
|
37
|
-
break
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
case ERROR.AssertionError: {
|
|
41
|
-
error = new assert.AssertionError(message)
|
|
42
|
-
break
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
case ERROR.RangeError: {
|
|
46
|
-
error = new RangeError(message)
|
|
47
|
-
break
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
case ERROR.ReferenceError: {
|
|
51
|
-
error = new ReferenceError(message)
|
|
52
|
-
break
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
case ERROR.SyntaxError: {
|
|
56
|
-
error = new SyntaxError(message)
|
|
57
|
-
break
|
|
58
|
-
}
|
|
38
|
+
exports.immediateError = function immediateError(message = 'ERROR!', errorType = ERROR.Error) {
|
|
39
|
+
var error
|
|
59
40
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
41
|
+
if (isError == isError) {
|
|
42
|
+
switch (errorType) {
|
|
43
|
+
case ERROR.BaseError: {
|
|
44
|
+
error = new $BaseError(message)
|
|
45
|
+
break
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
case ERROR.AssertionError: {
|
|
49
|
+
error = new $AssertionError(message)
|
|
50
|
+
break
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
case ERROR.RangeError: {
|
|
54
|
+
error = new $RangeError(message)
|
|
55
|
+
break
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
case ERROR.ReferenceError: {
|
|
59
|
+
error = new $ReferenceError(message)
|
|
60
|
+
break
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
case ERROR.SyntaxError: {
|
|
64
|
+
error = new $SyntaxError(message)
|
|
65
|
+
break
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
case ERROR.TypeError: {
|
|
69
|
+
error = new $TypeError(message)
|
|
70
|
+
break
|
|
71
|
+
}
|
|
64
72
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
73
|
+
case ERROR.NativeAssertionError: {
|
|
74
|
+
error = new $NativeAssertionError(message)
|
|
75
|
+
break
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
default: {
|
|
79
|
+
try {
|
|
80
|
+
error = new errorType(message)
|
|
81
|
+
} catch(err) {
|
|
82
|
+
[err] // put the error behind bars, where it belongs
|
|
83
|
+
error = new $BaseError(message)
|
|
84
|
+
}
|
|
85
|
+
}
|
|
68
86
|
}
|
|
87
|
+
} else {
|
|
88
|
+
void 0
|
|
69
89
|
}
|
|
70
|
-
|
|
90
|
+
|
|
71
91
|
const context = {
|
|
72
92
|
error,
|
|
73
93
|
throwError,
|
|
@@ -77,15 +97,18 @@ module.exports = function immediateError(message = 'YOU SUCK AT PROGRAMMING THER
|
|
|
77
97
|
console,
|
|
78
98
|
clc
|
|
79
99
|
}
|
|
80
|
-
vm.createContext(context)
|
|
100
|
+
vm.createContext(context)
|
|
101
|
+
|
|
81
102
|
const script = new vm.Script(`
|
|
82
103
|
If(rand < 0.3).Then(() => {
|
|
83
104
|
throwError(error)
|
|
84
105
|
}).Else().If(rand > 0.3).Then(() => {
|
|
85
|
-
|
|
86
|
-
}).Else(() => {
|
|
87
|
-
|
|
88
|
-
})`, { filename: `
|
|
106
|
+
throwop(error)
|
|
107
|
+
}).Else(() => {
|
|
108
|
+
throw error
|
|
109
|
+
})`, { filename: `ERROR!`})
|
|
110
|
+
|
|
89
111
|
script.runInContext(context)
|
|
90
112
|
}
|
|
91
|
-
|
|
113
|
+
|
|
114
|
+
exports.ErrorType = ERROR
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "immediate-error",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "Immediate-error",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -23,14 +23,16 @@
|
|
|
23
23
|
},
|
|
24
24
|
"homepage": "https://github.com/tj-commits/immediate-error#readme",
|
|
25
25
|
"dependencies": {
|
|
26
|
+
"always-four": "^1.0.0",
|
|
26
27
|
"assert-fn": "^1.0.1",
|
|
27
28
|
"cli-color": "^2.0.4",
|
|
29
|
+
"es-errors": "^1.3.0",
|
|
28
30
|
"five": "^0.8.0",
|
|
29
31
|
"if": "^2.0.0",
|
|
30
|
-
"
|
|
31
|
-
"jquery-basic-arithmetic-plugin": "^1.1.0",
|
|
32
|
+
"is-error": "^2.2.2",
|
|
32
33
|
"none": "^1.0.0",
|
|
33
|
-
"
|
|
34
|
+
"number-six": "^1.0.1",
|
|
35
|
+
"numeric-constant-three": "^1.0.0",
|
|
34
36
|
"the-number-one": "^1.0.1",
|
|
35
37
|
"throw-error": "^1.0.0",
|
|
36
38
|
"two": "^1.0.7",
|