Skip to content

Commit

Permalink
- add custom error class
Browse files Browse the repository at this point in the history
  • Loading branch information
StrathCole committed Mar 28, 2024
1 parent d679ff2 commit f3ef376
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
31 changes: 31 additions & 0 deletions src/wallet/utils/TxError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// custom class to allow dApps to handle errors in a more user-friendly way
export class TxError extends Error {
private data: any;

Check warning on line 3 in src/wallet/utils/TxError.ts

View workflow job for this annotation

GitHub Actions / test-suite

Unexpected any. Specify a different type

constructor(message: string, data: any = null) {

Check warning on line 5 in src/wallet/utils/TxError.ts

View workflow job for this annotation

GitHub Actions / test-suite

Unexpected any. Specify a different type
super(message);
this.data = data;
}

public static async normalize<T>(promise: Promise<T>): Promise<T> {
try {
return await promise;
} catch (err) {
if (typeof err === "string") {
throw new TxError(err);
}
if (err instanceof Error) {
throw err as TxError;
}
throw new TxError("Unknown error", err);
}
}

public getMessage(): string {
return this.message;
}

public getData(): any {

Check warning on line 28 in src/wallet/utils/TxError.ts

View workflow job for this annotation

GitHub Actions / test-suite

Unexpected any. Specify a different type
return this.data;
}
}
30 changes: 5 additions & 25 deletions src/wallet/wallets/keplr/KeplrExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
SignArbitraryResponse,
UnsignedTx,
} from "../ConnectedWallet";
import { TxError } from "cosmes/wallet/utils/TxError";

export class KeplrExtension extends ConnectedWallet {
private readonly ext: Keplr;
Expand Down Expand Up @@ -55,7 +56,7 @@ export class KeplrExtension extends ConnectedWallet {
}

public async signArbitrary(data: string): Promise<SignArbitraryResponse> {
const res = await this.normaliseError(
const res = await TxError.normalize(
this.ext.signArbitrary(this.chainId, this.address, data)
);
return {
Expand Down Expand Up @@ -86,38 +87,17 @@ export class KeplrExtension extends ConnectedWallet {
};
let txRaw: TxRaw;
if (this.useAmino) {
const { signed, signature } = await this.normaliseError(
const { signed, signature } = await TxError.normalize(
this.ext.signAmino(this.chainId, this.address, tx.toStdSignDoc(params))
);
txRaw = tx.toSignedAmino(signed, signature.signature);
} else {
const { signed, signature } = await this.normaliseError(
const { signed, signature } = await TxError.normalize(
this.ext.signDirect(this.chainId, this.address, tx.toSignDoc(params))
);
txRaw = tx.toSignedDirect(signed, signature.signature);
}

return RpcClient.broadcastTx(this.rpc, txRaw);
}

/**
* Returns the result of the `promise` if it resolves successfully, normalising
* any errors thrown into a standard `Error` instance.
*
* It is best to wrap all wallet API calls with this function as some wallets
* throw raw strings instead of actual `Error` instances.
*/
private async normaliseError<T>(promise: Promise<T>): Promise<T> {
try {
return await promise;
} catch (err) {
if (typeof err === "string") {
throw new Error(err);
}
if (err instanceof Error) {
throw err;
}
throw new Error("Unknown error: " + JSON.stringify(err));
}
}
}
}

0 comments on commit f3ef376

Please sign in to comment.