ac-custom-error 0.2.1 → 1.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/CHANGELOG.md +12 -0
- package/README.md +37 -3
- package/index.js +29 -1
- package/package.json +1 -1
- package/test.js +36 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,15 @@
|
|
|
1
|
+
<a name="1.0.0"></a>
|
|
2
|
+
|
|
3
|
+
# [1.0.0](https://github.com/admiralcloud/ac-custom-error/compare/v0.2.1..v1.0.0) (2023-03-12 13:22:39)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### Feature
|
|
7
|
+
|
|
8
|
+
* **App:** Add ACErrorFromCode | MP | [114a5fc289ac193d1374bcba9247917966b39f31](https://github.com/admiralcloud/ac-custom-error/commit/114a5fc289ac193d1374bcba9247917966b39f31)
|
|
9
|
+
You can now use a global library of error codes and throw the error based on a code.
|
|
10
|
+
Related issues: [undefined/undefined#master](undefined/browse/master)
|
|
11
|
+
## BREAKING CHANGES
|
|
12
|
+
* **App:** Two classes are exported in the function: ACError and ACErrorFromCode
|
|
1
13
|
<a name="0.2.1"></a>
|
|
2
14
|
|
|
3
15
|
## [0.2.1](https://github.com/admiralcloud/ac-custom-error/compare/v0.2.0..v0.2.1) (2023-03-12 11:50:21)
|
package/README.md
CHANGED
|
@@ -3,16 +3,50 @@ Extend the NodeJS error with an error code and additional information.
|
|
|
3
3
|
|
|
4
4
|
[](https://github.com/AdmiralCloud/ac-custom-error/actions/workflows/node.js.yml)
|
|
5
5
|
|
|
6
|
-
#
|
|
6
|
+
# ACErrorFromCode
|
|
7
|
+
This is the preferred way to use this class. You define a central library of error codes and export them to global. You can then throw the error based on that code.
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
```
|
|
11
|
+
const { ACErrorFromCode } = require('ac-custom-error)
|
|
12
|
+
|
|
13
|
+
// define error library
|
|
14
|
+
const errorCodes = {
|
|
15
|
+
12345: {
|
|
16
|
+
message: 'this is the error,
|
|
17
|
+
solution: 'Provide optional solution to solve the error',
|
|
18
|
+
stack: true // optional, if you want to log the stack
|
|
19
|
+
},
|
|
20
|
+
...
|
|
21
|
+
}
|
|
22
|
+
global.errorCodes = errorCodes
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
// call the error
|
|
26
|
+
throw new ACErrorFromCode(12345)
|
|
27
|
+
|
|
28
|
+
// result in console
|
|
29
|
+
ACErrorFromCode: this is the error
|
|
30
|
+
at STACK
|
|
31
|
+
code: 12345,
|
|
32
|
+
errorMessage: 'this is the error',
|
|
33
|
+
solution: 'Provide optional solution to solve the error'
|
|
34
|
+
}
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
# ACError
|
|
39
|
+
If you do not want ot use a global library of error codes, you can use ACError to still show code and additionalInfo.
|
|
40
|
+
## Usage
|
|
7
41
|
Use it just like the built-in error:
|
|
8
42
|
+ first parameter is the actual error message as string
|
|
9
43
|
+ second parameter is the error code
|
|
10
44
|
+ third parameter is an optional object of additional information (e.g. variables clarifying the error)
|
|
11
45
|
+ fouth parameter is an optional object of options that can help further processing of the error
|
|
12
46
|
|
|
13
|
-
|
|
47
|
+
### Example
|
|
14
48
|
```
|
|
15
|
-
const ACError = require('ac-custom-error')
|
|
49
|
+
const { ACError } = require('ac-custom-error')
|
|
16
50
|
|
|
17
51
|
throw new ACError('myError', 123, { id: 333 }, { stack: true })
|
|
18
52
|
|
package/index.js
CHANGED
|
@@ -22,5 +22,33 @@ class ACError extends Error {
|
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
+
class ACErrorFromCode extends Error {
|
|
26
|
+
constructor(code, additionalInfo = {}) {
|
|
27
|
+
super(code)
|
|
25
28
|
|
|
26
|
-
|
|
29
|
+
// fetch the error message for errorCodes object (if available)
|
|
30
|
+
const error = global?.errorCodes[code]
|
|
31
|
+
|
|
32
|
+
if (Error.captureStackTrace) {
|
|
33
|
+
Error.captureStackTrace(this, ACErrorFromCode)
|
|
34
|
+
}
|
|
35
|
+
// info
|
|
36
|
+
this.code = code || -1
|
|
37
|
+
this.errorMessage = error?.message || 'undefinedError'
|
|
38
|
+
this.message = this.errorMessage
|
|
39
|
+
|
|
40
|
+
if (error?.solution) {
|
|
41
|
+
this.solution = error.solution
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (Object.keys(additionalInfo).length) {
|
|
45
|
+
this.additionalInfo = additionalInfo
|
|
46
|
+
}
|
|
47
|
+
if (error?.stack) {
|
|
48
|
+
console.error(this.stack)
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
module.exports = { ACError, ACErrorFromCode }
|
package/package.json
CHANGED
package/test.js
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
const { expect } = require('chai')
|
|
2
2
|
|
|
3
|
-
const ACError = require('./index')
|
|
3
|
+
const { ACError, ACErrorFromCode } = require('./index')
|
|
4
4
|
|
|
5
5
|
const errorFunction = ({ message, code, additionalInfo, options }) => {
|
|
6
6
|
throw new ACError(message, code, additionalInfo, options )
|
|
7
7
|
}
|
|
8
8
|
|
|
9
|
+
const errorFunctionFromCode = ({ code, additionalInfo }) => {
|
|
10
|
+
throw new ACErrorFromCode(code, additionalInfo)
|
|
11
|
+
}
|
|
12
|
+
|
|
9
13
|
|
|
10
|
-
describe('Run tests', () => {
|
|
14
|
+
describe('Run tests with ACError', () => {
|
|
11
15
|
|
|
12
16
|
it('Check error code', () => {
|
|
13
17
|
let errorParams = { message: 'myError', code: 123 }
|
|
@@ -55,3 +59,33 @@ describe('Run tests', () => {
|
|
|
55
59
|
|
|
56
60
|
})
|
|
57
61
|
|
|
62
|
+
describe('Run tests with ACError', () => {
|
|
63
|
+
|
|
64
|
+
it('Define error codes', () => {
|
|
65
|
+
global.errorCodes = {
|
|
66
|
+
123: {
|
|
67
|
+
message: 'thisIsAnError',
|
|
68
|
+
solution: 'Provide an ID to avoid this error.'
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
it('Check error', () => {
|
|
75
|
+
let error
|
|
76
|
+
try {
|
|
77
|
+
errorFunctionFromCode({ code: 123 })
|
|
78
|
+
}
|
|
79
|
+
catch (err) {
|
|
80
|
+
error = err
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
expect(error).to.be.an('Error')
|
|
84
|
+
expect(error.message).to.equal(global.errorCodes[123].message)
|
|
85
|
+
expect(error.code).to.equal(123)
|
|
86
|
+
expect(error.solution).to.equal(global.errorCodes[123].solution)
|
|
87
|
+
})
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
})
|
|
91
|
+
|