Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom error class #54

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}
}
}
Loading