Skip to content

Commit 799b056

Browse files
authored
Fix error handling (#73)
* fix error handling * update deps * format
1 parent bb2b83e commit 799b056

File tree

4 files changed

+86
-63
lines changed

4 files changed

+86
-63
lines changed

eslint.config.js

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,44 @@
1-
module.exports = {
2-
languageOptions: {
3-
ecmaVersion: 'latest',
4-
sourceType: 'module',
5-
globals: {
6-
window: 'readonly',
7-
document: 'readonly',
8-
process: 'readonly',
9-
require: 'readonly',
1+
module.exports = [
2+
{
3+
languageOptions: {
4+
ecmaVersion: 'latest',
5+
sourceType: 'module',
6+
globals: {
7+
window: 'readonly',
8+
document: 'readonly',
9+
process: 'readonly',
10+
require: 'readonly',
11+
},
12+
parserOptions: {
13+
project: 'tsconfig.json',
14+
},
1015
},
11-
parserOptions: {
12-
project: 'tsconfig.json',
16+
ignores: ['dist/*', 'node_modules/*'],
17+
plugins: {
18+
'unused-imports': require('eslint-plugin-unused-imports'),
19+
'@typescript-eslint': require('@typescript-eslint/eslint-plugin'),
20+
'eslint-plugin-tsdoc': require('eslint-plugin-tsdoc'),
21+
},
22+
rules: {
23+
curly: 'warn',
24+
'prefer-const': 'warn',
25+
'no-else-return': 'warn',
26+
complexity: ['warn', 1000],
27+
'no-unneeded-ternary': 'warn',
28+
'no-alert': 'warn',
29+
'no-empty': 'warn',
30+
'no-useless-catch': 'error',
31+
'require-await': 'warn',
32+
'no-continue': 'warn',
33+
'no-console': 'warn',
34+
'unused-imports/no-unused-imports': 'warn',
35+
'no-magic-numbers': 'off',
1336
},
1437
},
15-
ignores: ['dist/*', 'node_modules/*'],
16-
plugins: {
17-
'unused-imports': require('eslint-plugin-unused-imports'),
18-
'@typescript-eslint': require('@typescript-eslint/eslint-plugin'),
19-
'eslint-plugin-tsdoc': require('eslint-plugin-tsdoc'),
20-
promise: require('eslint-plugin-promise'),
21-
},
22-
rules: {
23-
curly: 'warn',
24-
'prefer-const': 'warn',
25-
'no-else-return': 'warn',
26-
complexity: ['warn', 1000],
27-
'no-unneeded-ternary': 'warn',
28-
'no-alert': 'warn',
29-
'no-empty': 'warn',
30-
'no-useless-catch': 'error',
31-
'require-await': 'warn',
32-
'no-continue': 'warn',
33-
'promise/prefer-await-to-then': 'warn',
34-
'no-console': 'warn',
35-
'unused-imports/no-unused-imports': 'warn',
36-
'no-magic-numbers': 'off',
38+
{
39+
files: ['try.mjs'],
40+
rules: {
41+
'no-console': 'off',
42+
},
3743
},
38-
}
44+
]

package.json

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,33 @@
3434
"test": "yarn build && jest",
3535
"upgrade": "bunx npm-check-updates -i"
3636
},
37+
"resolutions": {
38+
"glob": "^11.0.0"
39+
},
3740
"dependencies": {
38-
"@ledgerhq/hw-transport": "6.31.4"
41+
"@ledgerhq/hw-transport": "6.31.9"
3942
},
4043
"devDependencies": {
41-
"@ledgerhq/hw-transport-mocker": "^6.28.6",
42-
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
43-
"@types/jest": "29.5.14",
44-
"@types/node": "^22.4.1",
45-
"@typescript-eslint/eslint-plugin": "^8.1.0",
46-
"@typescript-eslint/parser": "^8.14.0",
47-
"eslint": "^9.4.0",
48-
"eslint-config-prettier": "^9.1.0",
49-
"eslint-config-standard-with-typescript": "^43.0.1",
50-
"eslint-plugin-import": "^2.29.1",
51-
"eslint-plugin-n": "^17.8.1",
52-
"eslint-plugin-promise": "^7.1.0",
53-
"eslint-plugin-tsdoc": "^0.3.0",
54-
"eslint-plugin-unused-imports": "^4.0.0",
44+
"@ledgerhq/hw-transport-mocker": "^6.29.9",
45+
"@trivago/prettier-plugin-sort-imports": "^5.2.2",
46+
"@types/jest": "30.0.0",
47+
"@types/node": "^24.3.0",
48+
"@typescript-eslint/eslint-plugin": "^8.40.0",
49+
"@typescript-eslint/parser": "^8.40.0",
50+
"eslint": "^9.33.0",
51+
"eslint-config-love": "^121.0.0",
52+
"eslint-config-prettier": "^10.1.8",
53+
"eslint-plugin-import": "^2.32.0",
54+
"eslint-plugin-n": "^17.21.3",
55+
"eslint-plugin-promise": "^7.2.1",
56+
"eslint-plugin-tsdoc": "^0.4.0",
57+
"eslint-plugin-unused-imports": "^4.2.0",
5558
"jest": "^30.0.5",
56-
"prettier": "^3.3.2",
57-
"sort-package-json": "^2.10.1",
58-
"ts-jest": "^29.4.0",
59+
"prettier": "^3.6.2",
60+
"sort-package-json": "^3.4.0",
61+
"ts-jest": "^29.4.1",
5962
"ts-node": "^10.9.2",
60-
"typescript": "^5.4.5"
63+
"typescript": "^5.9.2"
6164
},
6265
"volta": {
6366
"node": "20.11.1",

src/app.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,19 +126,27 @@ export default class BaseApp {
126126

127127
const statusList = [LedgerError.NoErrors, LedgerError.DataIsInvalid, LedgerError.BadKeyHandle]
128128

129-
let responseBuffer
130-
131129
try {
132-
responseBuffer = await this.transport.send(this.CLA, ins, payloadType, p2, chunk, statusList)
130+
const responseBuffer = await this.transport.send(this.CLA, ins, payloadType, p2, chunk, statusList)
131+
return processResponse(responseBuffer, this.CUSTOM_APP_ERROR_DESCRIPTION)
133132
} catch (e) {
134-
// In case transport.send send throws an Error, we still want our custom ResponseError
135-
let statusCode: number = (e as any).statusCode || (e as any).returnCode
136-
const buffer = Buffer.alloc(2)
137-
buffer.writeUInt16BE(statusCode, 0)
133+
// Extract status code from error
134+
const statusCode: number = (e as any).statusCode || (e as any).returnCode || LedgerError.UnknownTransportError
135+
const message = (e as any).message
136+
137+
// Create buffer based on whether error has a message
138+
let buffer: Buffer
139+
if (message?.length > 0) {
140+
const messageBytes = Buffer.from(message, 'utf8')
141+
buffer = Buffer.concat([messageBytes, Buffer.allocUnsafe(2)])
142+
buffer.writeUInt16BE(statusCode, buffer.length - 2)
143+
} else {
144+
buffer = Buffer.allocUnsafe(2)
145+
buffer.writeUInt16BE(statusCode, 0)
146+
}
147+
138148
return processResponse(buffer, this.CUSTOM_APP_ERROR_DESCRIPTION)
139149
}
140-
141-
return processResponse(responseBuffer, this.CUSTOM_APP_ERROR_DESCRIPTION)
142150
}
143151

144152
/**

src/common.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,14 @@ export function processResponse(responseRaw: Buffer, customErrorList?: Record<Le
5858
}
5959

6060
// Append additional error message from payload if available
61+
// Avoid duplicating the error message if payload already contains it
6162
if (payload.length > 0) {
62-
errorMessage += ` : ${payload.toString('ascii')}`
63+
const payloadString = payload.toString('utf8')
64+
if (!payloadString.includes(errorMessage)) {
65+
errorMessage += ` : ${payloadString}`
66+
} else {
67+
errorMessage = payloadString
68+
}
6369
}
6470

6571
// Construct and throw an error object with details

0 commit comments

Comments
 (0)