Skip to content

Commit

Permalink
Merge pull request #62 from Zondax/dev
Browse files Browse the repository at this point in the history
New Release
  • Loading branch information
emmanuelm41 authored Nov 19, 2024
2 parents 2d2eb39 + 68dd807 commit fdba802
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 7 deletions.
Binary file modified bun.lockb
Binary file not shown.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@
"upgrade": "bunx npm-check-updates -i"
},
"dependencies": {
"@ledgerhq/hw-transport": "6.31.2"
"@ledgerhq/hw-transport": "6.31.4"
},
"devDependencies": {
"@ledgerhq/hw-transport-mocker": "^6.28.6",
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
"@types/jest": "29.5.12",
"@types/jest": "29.5.14",
"@types/node": "^22.4.1",
"@typescript-eslint/eslint-plugin": "^8.1.0",
"@typescript-eslint/parser": "^7.13.0",
"@typescript-eslint/parser": "^8.14.0",
"eslint": "^9.4.0",
"eslint-config-prettier": "^9.1.0",
"eslint-config-standard-with-typescript": "^43.0.1",
Expand All @@ -53,6 +53,7 @@
"eslint-plugin-tsdoc": "^0.3.0",
"eslint-plugin-unused-imports": "^4.0.0",
"prettier": "^3.3.2",
"sort-package-json": "^2.10.1",
"ts-jest": "^29.1.4",
"ts-node": "^10.9.2",
"typescript": "^5.4.5"
Expand Down
14 changes: 13 additions & 1 deletion src/common.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*****************************************************************************/
import { processErrorResponse } from './common'
import { LedgerError } from './consts'
import { LedgerCustomError, LedgerError } from './consts'
import { errorCodeToString } from './errors'
import { ResponseError } from './responseError'

Expand All @@ -25,6 +25,18 @@ describe('errorCodeToString', () => {
expect(errorCodeToString(knownErrorCode)).toEqual(expectedMessage)
})

it('should return the correct error message for a custom error code', () => {
const knownErrorCode: LedgerCustomError = 0xabcd
const expectedMessage = 'test custom error'
expect(errorCodeToString(knownErrorCode, { 0xabcd: expectedMessage })).toEqual(expectedMessage)
})

it('should return the correct error message for a known error code, when a custom error list is passed', () => {
const knownErrorCode: LedgerError = 0x9000
const expectedMessage = 'No errors'
expect(errorCodeToString(knownErrorCode, { [knownErrorCode]: 'Custom no errors' })).toEqual(expectedMessage)
})

it('should return "Unknown Return Code" for an unknown error code', () => {
const unknownErrorCode: LedgerError = 0x9999 as LedgerError
const expectedMessage = 'Unknown Return Code: 0x9999'
Expand Down
2 changes: 2 additions & 0 deletions src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export const PAYLOAD_TYPE: Readonly<Record<string, number>> = {
LAST: 0x02,
}

export type LedgerCustomError = number

// Ledger error codes and descriptions sorted by value
export enum LedgerError {
U2FUnknown = 1,
Expand Down
11 changes: 8 additions & 3 deletions src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,25 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*****************************************************************************/
import { ERROR_DESCRIPTION_OVERRIDE, LedgerError } from './consts'
import { ERROR_DESCRIPTION_OVERRIDE, LedgerCustomError, LedgerError } from './consts'

/**
* Converts a Ledger error code to a human-readable string.
*
* @param returnCode - The Ledger error code to convert.
* @param customErrorList - Custom error description list to convert error code with.
* @returns A string describing the error code.
*/
export function errorCodeToString(returnCode: LedgerError): string {
export function errorCodeToString(returnCode: LedgerError, customErrorList?: Record<LedgerCustomError, string>): string {
const returnCodeStr = returnCode.toString(16).toUpperCase()
let errDescription = `Unknown Return Code: 0x${returnCodeStr}`

if (returnCode in ERROR_DESCRIPTION_OVERRIDE) {
errDescription = ERROR_DESCRIPTION_OVERRIDE[returnCode]
return ERROR_DESCRIPTION_OVERRIDE[returnCode]
}

if (customErrorList && returnCode in customErrorList) {
return customErrorList[returnCode]
}

return errDescription
Expand Down

0 comments on commit fdba802

Please sign in to comment.