immediate-error 3.1.3 → 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.
Files changed (4) hide show
  1. package/README.md +33 -11
  2. package/index.d.ts +11 -0
  3. package/index.js +81 -74
  4. package/package.json +3 -4
package/README.md CHANGED
@@ -4,24 +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
- const { ERROR } = immediateError
9
+ immediateError() // this will throw a regular Error with the default message of "ERROR!"
10
10
 
11
- immediateError('Aaaaah') // this will throw an error with the message Aaaaah
11
+ immediateError('Aaaaah') // this will throw a regular Error with the message "Aaaaah"
12
12
 
13
- immediateError('Aaaaah', { errorType: ERROR.Error }) // does the same thing as above because by default errorType is ERROR.Error
13
+ immediateError('Aaaaah', ErrorType.BaseError) // does the same thing as above
14
14
 
15
- immediateError('Assertion error', { errorType: ERROR.AssertionError }) // throws an assertionerror
15
+ immediateError('Assertion error', ErrorType.AssertionError) // throws an AssertionError (from the assert-fn module)
16
16
 
17
- immediateError('Range error', { errorType: ERROR.RangeError }) // throws a rangeerror
17
+ immediateError('Assertion error', ErrorType.NativeAssertionError) // throws an AssertionError (from the node:assert module)
18
18
 
19
- immediateError('Reference error', { errorType: ERROR.ReferenceError }) // throws a referenceerror
19
+ immediateError('Range error', ErrorType.RangeError) // throws a RangeError
20
20
 
21
- immediateError('Syntax error', { errorType: ERROR.SyntaxError }) // throws a syntaxerror
21
+ immediateError('Reference error', ErrorType.ReferenceError) // throws a ReferenceError
22
22
 
23
- immediateError('type error', { errorType: ERROR.TypeError }) // throws a typeerror
23
+ immediateError('Syntax error', ErrorType.SyntaxError) // throws a SyntaxError
24
24
 
25
- immediateError('fucking error', { errorType: ERROR.FuckingError }) // throws a fuckingerror
25
+ immediateError('type error', ErrorType.TypeError) // throws a TypeError
26
26
 
27
- ```
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,88 +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 // number one
6
- const two = require('two') // no2
7
- const three = require('numeric-constant-three') //no3
8
- const four = require('always-four') //no4
9
- const five = require('five') //no5
10
- const six = require('number-six') //no6
11
- const successor = require('successor') // + 1
12
- global.jQuery = require('jquery')
13
- require('jquery-basic-arithmetic-plugin') // why not use jquery for math?
14
- const $ = jQuery // define jquery
15
- const { throwop } = require('yanoop') // throwop
16
- const throwError = require('throw-error') // nothing wrong with having another way to throw an error
17
- 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')
18
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
19
27
 
20
28
  const ERROR = Object.freeze({
21
- Error:0,
29
+ BaseError:0,
22
30
  AssertionError: one,
23
31
  RangeError: two(),
24
32
  ReferenceError: three(),
25
33
  SyntaxError: four(),
26
34
  TypeError: five(),
27
- FuckingError: six()
35
+ NativeAssertionError: six()
28
36
  })
29
37
 
30
- class FuckingError extends Error {
31
-
32
- constructor(message) {
33
- super(`FuckingError: Holy shit there was A Fucking Error With A Message: ${message}`)
34
- }
35
- }
36
-
37
- const assert = require('assert-fn') // import assert
38
- const vm = require('node:vm') // vm
39
-
40
- module.exports = function immediateError(message = 'YOU SUCK AT PROGRAMMING THERE WAS A FUCKING ERROR!', options = {
41
- errorType: ERROR.Error
42
- }) {
43
- var error = new Error(message) /* create an error
44
- variable and then use switch statement to set it*/
45
- switch(options.errorType) {
46
- case ERROR.Error: {
47
- error = new Error(message)
48
- break
49
- }
50
-
51
- case ERROR.AssertionError: {
52
- error = new assert.AssertionError(message)
53
- break
54
- }
55
-
56
- case ERROR.RangeError: {
57
- error = new RangeError(message)
58
- break
59
- }
60
-
61
- case ERROR.ReferenceError: {
62
- error = new ReferenceError(message)
63
- break
64
- }
65
-
66
- case ERROR.SyntaxError: {
67
- error = new SyntaxError(message)
68
- break
69
- }
70
-
71
- case ERROR.TypeError: {
72
- error = new TypeError(message)
73
- break
74
- }
38
+ exports.immediateError = function immediateError(message = 'ERROR!', errorType = ERROR.Error) {
39
+ var error
75
40
 
76
- case ERROR.FuckingError: {
77
- error = new FuckingError(message)
78
- break
79
- }
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
+ }
80
72
 
81
- default: {
82
- error = new Error(message)
83
- break
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
+ }
84
86
  }
87
+ } else {
88
+ void 0
85
89
  }
86
- // make a vm context
90
+
87
91
  const context = {
88
92
  error,
89
93
  throwError,
@@ -93,15 +97,18 @@ module.exports = function immediateError(message = 'YOU SUCK AT PROGRAMMING THER
93
97
  console,
94
98
  clc
95
99
  }
96
- vm.createContext(context) // Contextify the object.
100
+ vm.createContext(context)
101
+
97
102
  const script = new vm.Script(`
98
103
  If(rand < 0.3).Then(() => {
99
104
  throwError(error)
100
105
  }).Else().If(rand > 0.3).Then(() => {
101
- throwop(error)
102
- }).Else(() => {
103
- throw error
104
- })`, { filename: `YOU SUCK AT PROGRAMMING THERE WAS AN ERROR!`})
106
+ throwop(error)
107
+ }).Else(() => {
108
+ throw error
109
+ })`, { filename: `ERROR!`})
110
+
105
111
  script.runInContext(context)
106
112
  }
107
- module.exports.ERROR = ERROR
113
+
114
+ exports.ErrorType = ERROR
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "immediate-error",
3
- "version": "3.1.3",
3
+ "version": "4.0.0",
4
4
  "description": "Immediate-error",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -26,14 +26,13 @@
26
26
  "always-four": "^1.0.0",
27
27
  "assert-fn": "^1.0.1",
28
28
  "cli-color": "^2.0.4",
29
+ "es-errors": "^1.3.0",
29
30
  "five": "^0.8.0",
30
31
  "if": "^2.0.0",
31
- "jquery": "^3.7.1",
32
- "jquery-basic-arithmetic-plugin": "^1.1.0",
32
+ "is-error": "^2.2.2",
33
33
  "none": "^1.0.0",
34
34
  "number-six": "^1.0.1",
35
35
  "numeric-constant-three": "^1.0.0",
36
- "successor": "^1.0.1",
37
36
  "the-number-one": "^1.0.1",
38
37
  "throw-error": "^1.0.0",
39
38
  "two": "^1.0.7",