Skip to content

Commit 58248aa

Browse files
authored
feat: add argument to handle custom description errors on errorCodeToString (#61)
* fix: add missing dev dep * feat: add argument to handle custom description errors * feat: add tests
1 parent d59a312 commit 58248aa

File tree

5 files changed

+24
-4
lines changed

5 files changed

+24
-4
lines changed

bun.lockb

3.58 KB
Binary file not shown.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"eslint-plugin-tsdoc": "^0.3.0",
5454
"eslint-plugin-unused-imports": "^4.0.0",
5555
"prettier": "^3.3.2",
56+
"sort-package-json": "^2.10.1",
5657
"ts-jest": "^29.1.4",
5758
"ts-node": "^10.9.2",
5859
"typescript": "^5.4.5"

src/common.test.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*****************************************************************************/
1616
import { processErrorResponse } from './common'
17-
import { LedgerError } from './consts'
17+
import { LedgerCustomError, LedgerError } from './consts'
1818
import { errorCodeToString } from './errors'
1919
import { ResponseError } from './responseError'
2020

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

28+
it('should return the correct error message for a custom error code', () => {
29+
const knownErrorCode: LedgerCustomError = 0xabcd
30+
const expectedMessage = 'test custom error'
31+
expect(errorCodeToString(knownErrorCode, { 0xabcd: expectedMessage })).toEqual(expectedMessage)
32+
})
33+
34+
it('should return the correct error message for a known error code, when a custom error list is passed', () => {
35+
const knownErrorCode: LedgerError = 0x9000
36+
const expectedMessage = 'No errors'
37+
expect(errorCodeToString(knownErrorCode, { [knownErrorCode]: 'Custom no errors' })).toEqual(expectedMessage)
38+
})
39+
2840
it('should return "Unknown Return Code" for an unknown error code', () => {
2941
const unknownErrorCode: LedgerError = 0x9999 as LedgerError
3042
const expectedMessage = 'Unknown Return Code: 0x9999'

src/consts.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ export const PAYLOAD_TYPE: Readonly<Record<string, number>> = {
2525
LAST: 0x02,
2626
}
2727

28+
export type LedgerCustomError = number
29+
2830
// Ledger error codes and descriptions sorted by value
2931
export enum LedgerError {
3032
U2FUnknown = 1,

src/errors.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,25 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*****************************************************************************/
16-
import { ERROR_DESCRIPTION_OVERRIDE, LedgerError } from './consts'
16+
import { ERROR_DESCRIPTION_OVERRIDE, LedgerCustomError, LedgerError } from './consts'
1717

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

2829
if (returnCode in ERROR_DESCRIPTION_OVERRIDE) {
29-
errDescription = ERROR_DESCRIPTION_OVERRIDE[returnCode]
30+
return ERROR_DESCRIPTION_OVERRIDE[returnCode]
31+
}
32+
33+
if (customErrorList && returnCode in customErrorList) {
34+
return customErrorList[returnCode]
3035
}
3136

3237
return errDescription

0 commit comments

Comments
 (0)