@bitfinex/lib-util-err-js 0.0.0-stage → 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 ADDED
@@ -0,0 +1,13 @@
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
+
7
+ # 0.2.0
8
+ - feature: prevent adding prefix more than once
9
+
10
+ # 0.1.0
11
+ - feature: user error
12
+ - feature: generic error
13
+ - feature: api error handler
package/README.md CHANGED
@@ -1,3 +1,39 @@
1
- # Temporary Holding Version
1
+ # lib-util-err-js
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ The lib includes the utilities that help on resolving errors that should be shown to the endusers.
4
+
5
+ ## Installing
6
+
7
+ Requires Node.js >= 22.
8
+
9
+ ```console
10
+ npm i @bitfinex/lib-util-err-js
11
+ ```
12
+
13
+ ## Testing
14
+
15
+ ```console
16
+ npm test
17
+ npm run coverage
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ ```javascript
23
+ const { apiErrorHandler, GrcUserError, constants: { ERR_CODES } } = require('..')
24
+
25
+ const err = new Error('ERR_CONNECTION_FAILED')
26
+ const userErr = new GrcUserError('ERR_INVALID_CREDENTIALS', 'AUTH_ACTION', ERR_CODES.ERR_AUTH_FAIL)
27
+
28
+ const cb = (err) => {
29
+ console.error('- Response from action')
30
+ console.error(err)
31
+ }
32
+
33
+ apiErrorHandler(err, 'AUTH_FUNCTION', cb)
34
+ apiErrorHandler(userErr, 'AUTH_FUNC', cb)
35
+
36
+ ```
37
+
38
+ ## Authors
39
+ - vigan.abd
@@ -0,0 +1,14 @@
1
+ 'use strict'
2
+
3
+ const { apiErrorHandler, GrcUserError, constants: { ERR_CODES } } = require('..')
4
+
5
+ const err = new Error('ERR_CONNECTION_FAILED')
6
+ const userErr = new GrcUserError('ERR_INVALID_CREDENTIALS', 'AUTH_ACTION', ERR_CODES.ERR_AUTH_FAIL)
7
+
8
+ const cb = (err) => {
9
+ console.error('- Response from action')
10
+ console.error(err)
11
+ }
12
+
13
+ apiErrorHandler(err, 'AUTH_FUNCTION', cb)
14
+ apiErrorHandler(userErr, 'AUTH_FUNC', cb)
package/index.js ADDED
@@ -0,0 +1,11 @@
1
+ 'use strict'
2
+
3
+ const GrcUserError = require('./src/errors/grc.user.error')
4
+ const GrcGenericError = require('./src/errors/grc.generic.error')
5
+ const apiErrorHandler = require('./src/api.error.handler')
6
+
7
+ module.exports = {
8
+ GrcUserError,
9
+ GrcGenericError,
10
+ apiErrorHandler
11
+ }
package/package.json CHANGED
@@ -1,6 +1,49 @@
1
1
  {
2
2
  "name": "@bitfinex/lib-util-err-js",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "1.0.0",
4
+ "description": "Bitfinex lib utility for handling error messages",
5
+ "main": "index.js",
6
+ "engines": {
7
+ "node": ">=22.0"
8
+ },
9
+ "scripts": {
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"
16
+ },
17
+ "keywords": [
18
+ "bitfinex",
19
+ "utils",
20
+ "error"
21
+ ],
22
+ "author": {
23
+ "name": "vigan.abd",
24
+ "email": "vigan.abdurrahmani@bitfinex.com"
25
+ },
26
+ "license": "MIT",
27
+ "repository": {
28
+ "type": "git",
29
+ "url": "git+https://github.com/bitfinexcom/lib-util-err-js.git"
30
+ },
31
+ "maintainers": [
32
+ {
33
+ "name": "prdn",
34
+ "email": "paolo@bitfinex.com"
35
+ },
36
+ {
37
+ "name": "vigan.abd",
38
+ "email": "vigan.abdurrahmani@bitfinex.com"
39
+ }
40
+ ],
41
+ "bugs": {
42
+ "url": "https://github.com/bitfinexcom/lib-util-err-js/issues"
43
+ },
44
+ "homepage": "https://github.com/bitfinexcom/lib-util-err-js#readme",
45
+ "devDependencies": {
46
+ "@bitfinex/lint-js": "^1.1.0",
47
+ "brittle": "^4.5.0"
48
+ }
49
+ }
@@ -0,0 +1,49 @@
1
+ 'use strict'
2
+
3
+ const GrcUserError = require('./errors/grc.user.error')
4
+ const GrcGenericError = require('./errors/grc.generic.error')
5
+
6
+ /**
7
+ * @callback ErrorCallbackFunction
8
+ * @param {Error} err
9
+ * @returns {void}
10
+ */
11
+
12
+ /**
13
+ * Error handler utility that is useful for hiding internal errors from endusers
14
+ *
15
+ * @param {Error} err - Original error
16
+ * @param {string} [basename] - Optional, error base message
17
+ * @param {Object} [opts] - Optional, error handling options
18
+ * @param {boolean} [opts.force] - Optional, force original error message
19
+ * @param {ErrorCallbackFunction} cb - Callback function in format: `(err) => void`
20
+ */
21
+
22
+ const apiErrorHandler = (err, basename, opts, cb) => {
23
+ if (typeof basename === 'function') {
24
+ cb = basename
25
+ basename = null
26
+ opts = { force: false }
27
+ }
28
+
29
+ if (typeof opts === 'function') {
30
+ cb = opts
31
+ opts = { force: false }
32
+ }
33
+
34
+ console.error(new Date().toISOString(), basename, err)
35
+
36
+ if (err instanceof GrcUserError || opts.force) {
37
+ // in case if force flag is used, debugging purposes
38
+ let message = err.message || err.toString()
39
+ const prefix = basename && !message.startsWith(basename) ? `${basename}: ` : ''
40
+ message = prefix + message // wrap error with basename
41
+ const code = err.code || null
42
+
43
+ cb(new GrcUserError(message, code))
44
+ } else {
45
+ cb(new GrcGenericError(basename))
46
+ }
47
+ }
48
+
49
+ module.exports = apiErrorHandler
@@ -0,0 +1,20 @@
1
+ 'use strict'
2
+
3
+ const GrcUserError = require('./grc.user.error')
4
+
5
+ /**
6
+ * Generic api error, useful for hiding errors like connection failures
7
+ */
8
+ class GrcGenericError extends GrcUserError {
9
+ /**
10
+ * @param {string} [basename] - Optional, error message prefix
11
+ */
12
+ constructor (basename = null) {
13
+ super((basename ? `${basename}: ` : '') + 'generic error')
14
+
15
+ this.name = this.constructor.name
16
+ Error.captureStackTrace(this, this.constructor)
17
+ }
18
+ }
19
+
20
+ module.exports = GrcGenericError
@@ -0,0 +1,33 @@
1
+ 'use strict'
2
+
3
+ /**
4
+ * User friendly error that is shown directly to endusers
5
+ */
6
+ class GrcUserError extends Error {
7
+ /**
8
+ * @param {string} message - Error message
9
+ * @param {number} [code] - Optional, error code
10
+ */
11
+ constructor (message, code = null) {
12
+ super(message)
13
+
14
+ this.name = this.constructor.name
15
+ this.code = code
16
+
17
+ Error.captureStackTrace(this, this.constructor)
18
+ }
19
+
20
+ /**
21
+ * This method is called from JSON.stringify function when serializing object
22
+ * @returns {{ message: string, code: number, name: string }}
23
+ */
24
+ toJSON () {
25
+ return {
26
+ name: this.name,
27
+ message: this.message,
28
+ code: this.code
29
+ }
30
+ }
31
+ }
32
+
33
+ module.exports = GrcUserError
@@ -0,0 +1,93 @@
1
+ 'use strict'
2
+
3
+ const test = require('brittle')
4
+ const { format } = require('util')
5
+ const { GrcGenericError, GrcUserError, apiErrorHandler } = require('../')
6
+
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
+ }
15
+
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)
23
+ })
24
+ })
25
+
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)
33
+ })
34
+ })
35
+
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!
43
+ })
44
+ })
45
+
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)
53
+ })
54
+ })
55
+
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)
63
+ })
64
+ })
65
+
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'))
72
+ })
73
+ })
74
+
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)
82
+ })
83
+ })
84
+
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)
92
+ })
93
+ })
@@ -0,0 +1,35 @@
1
+ 'use strict'
2
+
3
+ const test = require('brittle')
4
+ const { GrcGenericError } = require('../')
5
+
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
+ })
16
+
17
+ test('GenericError: it should work also without optional params', (t) => {
18
+ const err = new GrcGenericError()
19
+
20
+ t.is(err.message, 'generic error')
21
+ t.is(err.code, null)
22
+ t.ok(err.stack.startsWith('GrcGenericError: generic error'))
23
+ })
24
+
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
+ }
33
+
34
+ t.alike(deserialized, expected)
35
+ })
@@ -0,0 +1,36 @@
1
+ 'use strict'
2
+
3
+ const test = require('brittle')
4
+ const { GrcUserError } = require('../')
5
+
6
+ test('GrcUserError: it should parse fields properly', (t) => {
7
+ const err = new GrcUserError('invalid user input', 10001)
8
+
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
+ })
17
+
18
+ test('GrcUserError: it should work also without optional params', (t) => {
19
+ const err = new GrcUserError('invalid user input')
20
+
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
+ })
25
+
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
+ }
34
+
35
+ t.alike(deserialized, expected)
36
+ })