@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 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 install --save https://github.com/bitfinexcom/lib-util-err-js.git
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.2.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
- "coverage": "nyc --reporter=html mocha",
8
- "format": "standard --fix",
9
- "lint": "standard",
10
- "test": "npm run lint && npm run unit",
11
- "unit": "NODE_ENV=test mocha ./test"
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
- "chai": "^4.3.4",
43
- "dirty-chai": "^2.0.1",
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
- /* eslint-env mocha */
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
- describe('apiErrorHandler tests', () => {
11
- let storedLog = ''
12
- const logStub = sinon.stub(console, 'error')
13
-
14
- before(() => {
15
- logStub.callsFake((...params) => {
16
- storedLog = format(...params)
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
- it('it should hide original error message in case if it\'s not instance of GrcUserError', (done) => {
29
- apiErrorHandler(new Error('Critical error in db'), 'MY_FUNCTION', {}, (err) => {
30
- expect(err).to.be.instanceOf(GrcGenericError)
31
- expect(err.message).to.be.equal('MY_FUNCTION: generic error')
32
- expect(err.code).to.be.equal(null)
33
- done()
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
- it('it should return original error message in case if it\'s instance of GrcUserError', (done) => {
38
- apiErrorHandler(new GrcUserError('invalid field', 10001), 'MY_FUNCTION', {}, (err) => {
39
- expect(err).to.be.instanceOf(GrcUserError)
40
- expect(err.message).to.be.equal('MY_FUNCTION: invalid field')
41
- expect(err.code).to.be.equal(10001)
42
- done()
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
- it('it should return original error message in case if it has force flag', (done) => {
47
- apiErrorHandler(new Error('Critical error in db'), 'MY_FUNCTION', { force: true }, (err) => {
48
- expect(err).to.be.instanceOf(GrcUserError)
49
- expect(err.message).to.be.equal('MY_FUNCTION: Critical error in db')
50
- expect(err.code).to.be.equal(null) // since it's missing add default one!
51
- done()
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
- it('it should work without optional params', (done) => {
56
- apiErrorHandler(new Error('Critical error in db'), (err) => {
57
- expect(err).to.be.instanceOf(GrcGenericError)
58
- expect(err.message).to.be.equal('generic error')
59
- expect(err.code).to.be.equal(null)
60
- done()
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
- it('it should work without opts param', (done) => {
65
- apiErrorHandler(new Error('Critical error in db'), 'MY_FUNCTION', (err) => {
66
- expect(err).to.be.instanceOf(GrcGenericError)
67
- expect(err.message).to.be.equal('MY_FUNCTION: generic error')
68
- expect(err.code).to.be.equal(null)
69
- done()
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
- it('it should always log original error', (done) => {
74
- apiErrorHandler(new GrcUserError('invalid field', 10001), (err) => {
75
- expect(storedLog).not.to.be.null()
76
- expect(storedLog.includes(err.trace))
77
- done()
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
- it('it should use toString method when error does not have message field', (done) => {
82
- apiErrorHandler('Critical error in db', 'MY_FUNCTION', { force: true }, (err) => {
83
- expect(err).to.be.instanceOf(GrcUserError)
84
- expect(err.message).to.be.equal('MY_FUNCTION: Critical error in db')
85
- expect(err.code).to.be.equal(null)
86
- done()
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
- it('it should avoid adding prefix more than once', (done) => {
91
- apiErrorHandler(new GrcUserError('MY_FUNCTION: invalid field'), 'MY_FUNCTION', (err) => {
92
- expect(err).to.be.instanceOf(GrcUserError)
93
- expect(err.message).to.be.equal('MY_FUNCTION: invalid field')
94
- expect(err.code).to.be.equal(null)
95
- done()
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
- /* eslint-env mocha */
4
-
5
- const { expect } = require('chai').use(require('dirty-chai'))
3
+ const test = require('brittle')
6
4
  const { GrcGenericError } = require('../')
7
5
 
8
- describe('GenericError tests', () => {
9
- it('it should parse fields properly', () => {
10
- const err = new GrcGenericError('MY_FUNCTION')
11
- expect(err).to.be.instanceOf(Error)
12
- expect(err.name).to.be.equal('GrcGenericError')
13
- expect(err.message).to.be.equal('MY_FUNCTION: generic error')
14
- expect(err.code).to.be.equal(null)
15
- expect(err.stack).to.be.a('string')
16
- expect(err.stack.startsWith('GrcGenericError: MY_FUNCTION: generic error')).to.be.true()
17
- expect(err.stack.includes(' at ')).to.be.true()
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
- it('it should work also without optional params', () => {
21
- const err = new GrcGenericError()
17
+ test('GenericError: it should work also without optional params', (t) => {
18
+ const err = new GrcGenericError()
22
19
 
23
- expect(err.message).to.be.equal('generic error')
24
- expect(err.code).to.be.equal(null)
25
- expect(err.stack.startsWith('GrcGenericError: generic error')).to.be.true()
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
- it('it should serialize only specific fields', () => {
29
- const serialized = JSON.stringify(new GrcGenericError('MY_FUNC'))
30
- const deserialized = JSON.parse(serialized)
31
- const expected = {
32
- name: 'GrcGenericError',
33
- message: 'MY_FUNC: generic error',
34
- code: null
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
- expect(deserialized).to.be.eqls(expected)
38
- })
34
+ t.alike(deserialized, expected)
39
35
  })
@@ -1,40 +1,36 @@
1
1
  'use strict'
2
2
 
3
- /* eslint-env mocha */
4
-
5
- const { expect } = require('chai').use(require('dirty-chai'))
3
+ const test = require('brittle')
6
4
  const { GrcUserError } = require('../')
7
5
 
8
- describe('GrcUserError tests', () => {
9
- it('it should parse fields properly', () => {
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
- expect(err).to.be.instanceOf(Error)
13
- expect(err.name).to.be.equal('GrcUserError')
14
- expect(err.message).to.be.equal('invalid user input')
15
- expect(err.code).to.be.equal(10001)
16
- expect(err.stack).to.be.a('string')
17
- expect(err.stack.startsWith('GrcUserError: invalid user input')).to.be.true()
18
- expect(err.stack.includes(' at ')).to.be.true()
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
- it('it should work also without optional params', () => {
22
- const err = new GrcUserError('invalid user input')
18
+ test('GrcUserError: it should work also without optional params', (t) => {
19
+ const err = new GrcUserError('invalid user input')
23
20
 
24
- expect(err.message).to.be.equal('invalid user input')
25
- expect(err.code).to.be.null()
26
- expect(err.stack.startsWith('GrcUserError: invalid user input')).to.be.true()
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
- it('it should serialize only specific fields', () => {
30
- const serialized = JSON.stringify(new GrcUserError('invalid user input', 10001))
31
- const deserialized = JSON.parse(serialized)
32
- const expected = {
33
- name: 'GrcUserError',
34
- message: 'invalid user input',
35
- code: 10001
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
- expect(deserialized).to.be.eqls(expected)
39
- })
35
+ t.alike(deserialized, expected)
40
36
  })