ac-custom-error 0.1.0 → 0.2.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 CHANGED
@@ -1,3 +1,13 @@
1
+ <a name="0.2.0"></a>
2
+
3
+ # [0.2.0](https://github.com/admiralcloud/ac-custom-error/compare/v0.1.0..v0.2.0) (2023-03-12 11:47:33)
4
+
5
+
6
+ ### Feature
7
+
8
+ * **App:** Enhance error with options | MP | [53ada866bf85e3c9a9f50583efd1f9f11debab50](https://github.com/admiralcloud/ac-custom-error/commit/53ada866bf85e3c9a9f50583efd1f9f11debab50)
9
+ In addition to additionalInfo, the error can now also contain options. Use options for further error processing in you app.
10
+ Related issues: [undefined/undefined#master](undefined/browse/master)
1
11
  <a name="0.1.0"></a>
2
12
 
3
13
  # [0.1.0](https://github.com/admiralcloud/ac-custom-error/compare/..v0.1.0) (2023-03-10 13:45:10)
package/README.md CHANGED
@@ -4,21 +4,47 @@ Extend the NodeJS error with an error code and additional information.
4
4
  [![Node.js CI](https://github.com/AdmiralCloud/ac-custom-error/actions/workflows/node.js.yml/badge.svg)](https://github.com/AdmiralCloud/ac-custom-error/actions/workflows/node.js.yml)
5
5
 
6
6
  # Usage
7
+ Use it just like the built-in error:
8
+ + first parameter is the actual error message as string
9
+ + second parameter is the error code
10
+ + third parameter is an optional object of additional information (e.g. variables clarifying the error)
11
+ + fouth parameter is an optional object of options that can help further processing of the error
12
+
13
+ ## Example
7
14
  ```
8
15
  const ACError = require('ac-custom-error')
9
16
 
10
- throw new ACError('myError', 123, { id: 333 })
17
+ throw new ACError('myError', 123, { id: 333 }, { stack: true })
11
18
 
12
19
  // OUTPUT
13
20
  ACError: myError
14
21
  at STACK
15
22
  code: 123,
16
23
  errorMessage: 'myError',
17
- additionalInfo: { id: 333 }
24
+ additionalInfo: { id: 333 },
25
+ options: { stack: true }
18
26
  }
19
27
 
20
28
  ```
21
29
 
30
+ ### How to use options
31
+ You might have an app were you would like to distinguish between soft errors (e.g. a requested ID is not available) and hard errors (that are bugs in the code). The first one should be logged as information in the code (maybe not even on ERROR level). The second one, the hard error, should probably contain as much information as possible for further debugging, so you want to log the stack as well.
32
+
33
+ ```
34
+ // EXAMPLE soft error
35
+ throw new Error('userIdInvalid', 10223, { id: 12345 })
36
+ // will e.g. log like this
37
+ WARN | userIdInvalid | 10223 | { id: 12345 }
38
+
39
+ // EXAMPLE hard error
40
+ throw new Error('SQLinvalid', 13001, { query: 'SELECT FROM a' }, { stack: true })
41
+ // will e.g. log like this
42
+ ACError: SQLinvalid
43
+ at SQLconnect (/pathTofile:150:10)
44
+ ...
45
+ ERROR | SQLinvalid | 13001 | { query: 'SELECT FROM a' }
46
+ ```
47
+
22
48
 
23
49
  # Links
24
50
  - [Website](https://www.admiralcloud.com/)
package/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  class ACError extends Error {
2
- constructor(message, code, additionalInfo = {}) {
2
+ constructor(message, code, additionalInfo = {}, options = {}) {
3
3
  super(message)
4
4
 
5
5
  if (Error.captureStackTrace) {
@@ -8,10 +8,14 @@ class ACError extends Error {
8
8
  // info
9
9
  this.code = code || -1
10
10
  this.errorMessage = message
11
- // show other properties of options object
11
+ // show other properties of the error (usually additional payload parameters that clarify the error)
12
12
  if (Object.keys(additionalInfo).length) {
13
13
  this.additionalInfo = additionalInfo
14
14
  }
15
+ // options
16
+ if (Object.keys(options).length) {
17
+ this.options = options
18
+ }
15
19
  }
16
20
  }
17
21
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ac-custom-error",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
4
  "description": "Custom NodeJS error with error code and additional info",
5
5
  "main": "index.js",
6
6
  "repository": "https://github.com/AdmiralCloud/ac-custom-error",
package/test.js CHANGED
@@ -2,8 +2,8 @@ const { expect } = require('chai')
2
2
 
3
3
  const ACError = require('./index')
4
4
 
5
- const errorFunction = ({ message, code, additionalInfo }) => {
6
- throw new ACError(message, code, additionalInfo)
5
+ const errorFunction = ({ message, code, additionalInfo, options }) => {
6
+ throw new ACError(message, code, additionalInfo, options )
7
7
  }
8
8
 
9
9
 
@@ -38,5 +38,20 @@ describe('Run tests', () => {
38
38
  expect(error.additionalInfo.id).to.equal(errorParams.additionalInfo.id)
39
39
  })
40
40
 
41
+ it('Check that options are available', () => {
42
+ let errorParams = { message: 'myError', code: 123, additionalInfo: {}, options: { stack: true } }
43
+ let error
44
+ try {
45
+ errorFunction(errorParams)
46
+ }
47
+ catch (err) {
48
+ error = err
49
+ }
50
+ expect(error).to.be.an('Error')
51
+ expect(error.message).to.equal(errorParams.message)
52
+ expect(error.code).to.equal(errorParams.code)
53
+ expect(error.options).to.have.property('stack', errorParams.options.stack)
54
+ })
55
+
41
56
  })
42
57