Skip to content

Commit

Permalink
Merge pull request #122 from oasisprotocol/pro-wh/feature/ledgercode
Browse files Browse the repository at this point in the history
ts-web/signer-ledger: preserve original return code
  • Loading branch information
pro-wh authored May 5, 2021
2 parents 82812f1 + d8e85f2 commit a8ede39
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions client-sdk/ts-web/signer-ledger/docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
New features:

- The new `LedgerContextSigner.fromTransport` lets you bring your own transport object.
- Errors from Ledger now come as `LedgerCodeError` with a `returnCode` property.

Bug fixes:

Expand Down
16 changes: 14 additions & 2 deletions client-sdk/ts-web/signer-ledger/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,21 @@ function bufFromU8(u8: Uint8Array) {
return Buffer.from(u8.buffer, u8.byteOffset, u8.byteLength);
}

export class LedgerCodeError extends Error {
returnCode: number;
errorMessage: string;

constructor(message: string, return_code: number, error_message: string) {
super(`${message}: ${return_code} ${error_message}`);
this.returnCode = return_code;
this.errorMessage = error_message;
}
}

function successOrThrow(response: Response, message: string) {
if (response.return_code !== 0x9000)
throw new Error(`${message}: ${response.return_code} ${response.error_message}`);
if (response.return_code !== 0x9000) {
throw new LedgerCodeError(message, response.return_code, response.error_message);
}
return response;
}

Expand Down

0 comments on commit a8ede39

Please sign in to comment.