@bitfinex/lib-util-err-js 0.2.0 → 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 +6 -0
- package/README.md +4 -1
- package/package.json +12 -12
- package/test/api.error.handler.test.js +71 -76
- package/test/generic.error.test.js +26 -30
- package/test/user.error.test.js +26 -30
package/CHANGELOG.md
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
# 1.0.0
|
|
2
|
+
- breaking: require Node.js >= 22
|
|
3
|
+
- chore: commit package-lock.json and disable install scripts via .npmrc
|
|
4
|
+
- chore: replace standard with @bitfinex/lint-js
|
|
5
|
+
- chore: replace mocha/chai/sinon/nyc with brittle
|
|
6
|
+
|
|
1
7
|
# 0.2.0
|
|
2
8
|
- feature: prevent adding prefix more than once
|
|
3
9
|
|
package/README.md
CHANGED
|
@@ -4,14 +4,17 @@ The lib includes the utilities that help on resolving errors that should be show
|
|
|
4
4
|
|
|
5
5
|
## Installing
|
|
6
6
|
|
|
7
|
+
Requires Node.js >= 22.
|
|
8
|
+
|
|
7
9
|
```console
|
|
8
|
-
npm
|
|
10
|
+
npm i @bitfinex/lib-util-err-js
|
|
9
11
|
```
|
|
10
12
|
|
|
11
13
|
## Testing
|
|
12
14
|
|
|
13
15
|
```console
|
|
14
16
|
npm test
|
|
17
|
+
npm run coverage
|
|
15
18
|
```
|
|
16
19
|
|
|
17
20
|
## Usage
|
package/package.json
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bitfinex/lib-util-err-js",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"description": "Bitfinex lib utility for handling error messages",
|
|
5
5
|
"main": "index.js",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=22.0"
|
|
8
|
+
},
|
|
6
9
|
"scripts": {
|
|
7
|
-
"
|
|
8
|
-
"
|
|
9
|
-
"
|
|
10
|
-
"test": "
|
|
11
|
-
"
|
|
10
|
+
"lint": "bfx-lint-js",
|
|
11
|
+
"lint:fix": "bfx-lint-js --fix",
|
|
12
|
+
"test": "npm run lint && npm run test:unit",
|
|
13
|
+
"test:unit": "brittle-node test/*.test.js",
|
|
14
|
+
"coverage": "npm run lint && npm run coverage:unit",
|
|
15
|
+
"coverage:unit": "brittle-node --coverage test/*.test.js"
|
|
12
16
|
},
|
|
13
17
|
"keywords": [
|
|
14
18
|
"bitfinex",
|
|
@@ -39,11 +43,7 @@
|
|
|
39
43
|
},
|
|
40
44
|
"homepage": "https://github.com/bitfinexcom/lib-util-err-js#readme",
|
|
41
45
|
"devDependencies": {
|
|
42
|
-
"
|
|
43
|
-
"
|
|
44
|
-
"mocha": "^9.1.3",
|
|
45
|
-
"nyc": "^15.1.0",
|
|
46
|
-
"sinon": "^12.0.1",
|
|
47
|
-
"standard": "^16.0.4"
|
|
46
|
+
"@bitfinex/lint-js": "^1.1.0",
|
|
47
|
+
"brittle": "^4.5.0"
|
|
48
48
|
}
|
|
49
49
|
}
|
|
@@ -1,98 +1,93 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const sinon = require('sinon')
|
|
6
|
-
const { expect } = require('chai').use(require('dirty-chai'))
|
|
3
|
+
const test = require('brittle')
|
|
7
4
|
const { format } = require('util')
|
|
8
5
|
const { GrcGenericError, GrcUserError, apiErrorHandler } = require('../')
|
|
9
6
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
})
|
|
19
|
-
|
|
20
|
-
after(() => {
|
|
21
|
-
logStub.restore()
|
|
22
|
-
})
|
|
23
|
-
|
|
24
|
-
beforeEach(() => {
|
|
25
|
-
storedLog = ''
|
|
26
|
-
})
|
|
7
|
+
// Captures console.error output for the test and restores it afterwards
|
|
8
|
+
const stubConsoleError = (t) => {
|
|
9
|
+
const logs = []
|
|
10
|
+
const original = console.error
|
|
11
|
+
console.error = (...params) => logs.push(format(...params))
|
|
12
|
+
t.teardown(() => { console.error = original })
|
|
13
|
+
return logs
|
|
14
|
+
}
|
|
27
15
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
16
|
+
test('it should hide original error message in case if it\'s not instance of GrcUserError', (t) => {
|
|
17
|
+
t.plan(3)
|
|
18
|
+
stubConsoleError(t)
|
|
19
|
+
apiErrorHandler(new Error('Critical error in db'), 'MY_FUNCTION', {}, (err) => {
|
|
20
|
+
t.ok(err instanceof GrcGenericError)
|
|
21
|
+
t.is(err.message, 'MY_FUNCTION: generic error')
|
|
22
|
+
t.is(err.code, null)
|
|
35
23
|
})
|
|
24
|
+
})
|
|
36
25
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
26
|
+
test('it should return original error message in case if it\'s instance of GrcUserError', (t) => {
|
|
27
|
+
t.plan(3)
|
|
28
|
+
stubConsoleError(t)
|
|
29
|
+
apiErrorHandler(new GrcUserError('invalid field', 10001), 'MY_FUNCTION', {}, (err) => {
|
|
30
|
+
t.ok(err instanceof GrcUserError)
|
|
31
|
+
t.is(err.message, 'MY_FUNCTION: invalid field')
|
|
32
|
+
t.is(err.code, 10001)
|
|
44
33
|
})
|
|
34
|
+
})
|
|
45
35
|
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
36
|
+
test('it should return original error message in case if it has force flag', (t) => {
|
|
37
|
+
t.plan(3)
|
|
38
|
+
stubConsoleError(t)
|
|
39
|
+
apiErrorHandler(new Error('Critical error in db'), 'MY_FUNCTION', { force: true }, (err) => {
|
|
40
|
+
t.ok(err instanceof GrcUserError)
|
|
41
|
+
t.is(err.message, 'MY_FUNCTION: Critical error in db')
|
|
42
|
+
t.is(err.code, null) // since it's missing add default one!
|
|
53
43
|
})
|
|
44
|
+
})
|
|
54
45
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
46
|
+
test('it should work without optional params', (t) => {
|
|
47
|
+
t.plan(3)
|
|
48
|
+
stubConsoleError(t)
|
|
49
|
+
apiErrorHandler(new Error('Critical error in db'), (err) => {
|
|
50
|
+
t.ok(err instanceof GrcGenericError)
|
|
51
|
+
t.is(err.message, 'generic error')
|
|
52
|
+
t.is(err.code, null)
|
|
62
53
|
})
|
|
54
|
+
})
|
|
63
55
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
56
|
+
test('it should work without opts param', (t) => {
|
|
57
|
+
t.plan(3)
|
|
58
|
+
stubConsoleError(t)
|
|
59
|
+
apiErrorHandler(new Error('Critical error in db'), 'MY_FUNCTION', (err) => {
|
|
60
|
+
t.ok(err instanceof GrcGenericError)
|
|
61
|
+
t.is(err.message, 'MY_FUNCTION: generic error')
|
|
62
|
+
t.is(err.code, null)
|
|
71
63
|
})
|
|
64
|
+
})
|
|
72
65
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
66
|
+
test('it should always log original error', (t) => {
|
|
67
|
+
t.plan(2)
|
|
68
|
+
const logs = stubConsoleError(t)
|
|
69
|
+
apiErrorHandler(new GrcUserError('invalid field', 10001), () => {
|
|
70
|
+
t.is(logs.length, 1)
|
|
71
|
+
t.ok(logs[0].includes('GrcUserError: invalid field'))
|
|
79
72
|
})
|
|
73
|
+
})
|
|
80
74
|
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
75
|
+
test('it should use toString method when error does not have message field', (t) => {
|
|
76
|
+
t.plan(3)
|
|
77
|
+
stubConsoleError(t)
|
|
78
|
+
apiErrorHandler('Critical error in db', 'MY_FUNCTION', { force: true }, (err) => {
|
|
79
|
+
t.ok(err instanceof GrcUserError)
|
|
80
|
+
t.is(err.message, 'MY_FUNCTION: Critical error in db')
|
|
81
|
+
t.is(err.code, null)
|
|
88
82
|
})
|
|
83
|
+
})
|
|
89
84
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
85
|
+
test('it should avoid adding prefix more than once', (t) => {
|
|
86
|
+
t.plan(3)
|
|
87
|
+
stubConsoleError(t)
|
|
88
|
+
apiErrorHandler(new GrcUserError('MY_FUNCTION: invalid field'), 'MY_FUNCTION', (err) => {
|
|
89
|
+
t.ok(err instanceof GrcUserError)
|
|
90
|
+
t.is(err.message, 'MY_FUNCTION: invalid field')
|
|
91
|
+
t.is(err.code, null)
|
|
97
92
|
})
|
|
98
93
|
})
|
|
@@ -1,39 +1,35 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const { expect } = require('chai').use(require('dirty-chai'))
|
|
3
|
+
const test = require('brittle')
|
|
6
4
|
const { GrcGenericError } = require('../')
|
|
7
5
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
})
|
|
6
|
+
test('GenericError: it should parse fields properly', (t) => {
|
|
7
|
+
const err = new GrcGenericError('MY_FUNCTION')
|
|
8
|
+
t.ok(err instanceof Error)
|
|
9
|
+
t.is(err.name, 'GrcGenericError')
|
|
10
|
+
t.is(err.message, 'MY_FUNCTION: generic error')
|
|
11
|
+
t.is(err.code, null)
|
|
12
|
+
t.is(typeof err.stack, 'string')
|
|
13
|
+
t.ok(err.stack.startsWith('GrcGenericError: MY_FUNCTION: generic error'))
|
|
14
|
+
t.ok(err.stack.includes(' at '))
|
|
15
|
+
})
|
|
19
16
|
|
|
20
|
-
|
|
21
|
-
|
|
17
|
+
test('GenericError: it should work also without optional params', (t) => {
|
|
18
|
+
const err = new GrcGenericError()
|
|
22
19
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
20
|
+
t.is(err.message, 'generic error')
|
|
21
|
+
t.is(err.code, null)
|
|
22
|
+
t.ok(err.stack.startsWith('GrcGenericError: generic error'))
|
|
23
|
+
})
|
|
27
24
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
25
|
+
test('GenericError: it should serialize only specific fields', (t) => {
|
|
26
|
+
const serialized = JSON.stringify(new GrcGenericError('MY_FUNC'))
|
|
27
|
+
const deserialized = JSON.parse(serialized)
|
|
28
|
+
const expected = {
|
|
29
|
+
name: 'GrcGenericError',
|
|
30
|
+
message: 'MY_FUNC: generic error',
|
|
31
|
+
code: null
|
|
32
|
+
}
|
|
36
33
|
|
|
37
|
-
|
|
38
|
-
})
|
|
34
|
+
t.alike(deserialized, expected)
|
|
39
35
|
})
|
package/test/user.error.test.js
CHANGED
|
@@ -1,40 +1,36 @@
|
|
|
1
1
|
'use strict'
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const { expect } = require('chai').use(require('dirty-chai'))
|
|
3
|
+
const test = require('brittle')
|
|
6
4
|
const { GrcUserError } = require('../')
|
|
7
5
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const err = new GrcUserError('invalid user input', 10001)
|
|
6
|
+
test('GrcUserError: it should parse fields properly', (t) => {
|
|
7
|
+
const err = new GrcUserError('invalid user input', 10001)
|
|
11
8
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
9
|
+
t.ok(err instanceof Error)
|
|
10
|
+
t.is(err.name, 'GrcUserError')
|
|
11
|
+
t.is(err.message, 'invalid user input')
|
|
12
|
+
t.is(err.code, 10001)
|
|
13
|
+
t.is(typeof err.stack, 'string')
|
|
14
|
+
t.ok(err.stack.startsWith('GrcUserError: invalid user input'))
|
|
15
|
+
t.ok(err.stack.includes(' at '))
|
|
16
|
+
})
|
|
20
17
|
|
|
21
|
-
|
|
22
|
-
|
|
18
|
+
test('GrcUserError: it should work also without optional params', (t) => {
|
|
19
|
+
const err = new GrcUserError('invalid user input')
|
|
23
20
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
21
|
+
t.is(err.message, 'invalid user input')
|
|
22
|
+
t.is(err.code, null)
|
|
23
|
+
t.ok(err.stack.startsWith('GrcUserError: invalid user input'))
|
|
24
|
+
})
|
|
28
25
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
26
|
+
test('GrcUserError: it should serialize only specific fields', (t) => {
|
|
27
|
+
const serialized = JSON.stringify(new GrcUserError('invalid user input', 10001))
|
|
28
|
+
const deserialized = JSON.parse(serialized)
|
|
29
|
+
const expected = {
|
|
30
|
+
name: 'GrcUserError',
|
|
31
|
+
message: 'invalid user input',
|
|
32
|
+
code: 10001
|
|
33
|
+
}
|
|
37
34
|
|
|
38
|
-
|
|
39
|
-
})
|
|
35
|
+
t.alike(deserialized, expected)
|
|
40
36
|
})
|