Skip to content

Commit 1ec463a

Browse files
committed
fix: improve error codes
1 parent ab9c06c commit 1ec463a

File tree

11 files changed

+29
-27
lines changed

11 files changed

+29
-27
lines changed

packages/dapp/core/src/clients/base/base.client.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { MsgAddLAK, MsgSignWithFAK, MsgSignIn, Permissions, MsgSignInQueryParams, Codec } from "@one-click-connect/core";
22
import { BaseDAppClientConfig } from "./base.client.config";
3-
import { Account } from "../../common";
3+
import { Account, ErrorCodes } from "../../common";
44
import { AccountStore, Callbacks, PendingTransactionStore, Provider } from "../../common";
55

66
export abstract class BaseDAppClient<Transaction, TransactionResult, Config extends BaseDAppClientConfig = BaseDAppClientConfig> {
@@ -21,18 +21,18 @@ export abstract class BaseDAppClient<Transaction, TransactionResult, Config exte
2121
}
2222

2323
get accountId() {
24-
if (!this.connected) throw new Error("Not connected");
24+
if (!this.connected) throw new Error(ErrorCodes.NOT_CONNECTED);
2525
return this.account!.accountId;
2626
}
2727

2828
get accessKey() {
29-
if (!this.connected) throw new Error("Not connected");
29+
if (!this.connected) throw new Error(ErrorCodes.NOT_CONNECTED);
3030
return this.account!.accessKey;
3131
}
3232

3333
get publicKey() {
34-
if (!this.connected) throw new Error("Not connected");
35-
if (!this.accessKey) throw new Error("Access key undefined");
34+
if (!this.connected) throw new Error(ErrorCodes.NOT_CONNECTED);
35+
if (!this.accessKey) throw new Error(ErrorCodes.NO_ACCESS_KEY);
3636
return this.provider.derivePublicKey(this.accessKey);
3737
}
3838

@@ -55,7 +55,7 @@ export abstract class BaseDAppClient<Transaction, TransactionResult, Config exte
5555
account = await this.accountStore.get();
5656
}
5757
if (!account) {
58-
throw new Error("Could not sign in using One Click Connect, no msgSignIn detected and no account stored");
58+
throw new Error(ErrorCodes.COULD_NOT_CONNECT);
5959
}
6060
this.account = account;
6161
if (executePendingTransactions) await this.executePendingTransactions();
@@ -68,7 +68,7 @@ export abstract class BaseDAppClient<Transaction, TransactionResult, Config exte
6868
* @returns The transaction result or void if executed a callback.
6969
*/
7070
async signAndSendTransaction(transaction: Transaction): Promise<TransactionResult | void> {
71-
if (!this.connected) throw new Error("Not connected");
71+
if (!this.connected) throw new Error(ErrorCodes.NOT_CONNECTED);
7272
const hasAccessKey = this.account!.accessKey !== undefined;
7373
const canExecute = await this.provider.canExecute(this.permissions, transaction);
7474
const hasPermissions =
@@ -100,20 +100,20 @@ export abstract class BaseDAppClient<Transaction, TransactionResult, Config exte
100100
* @returns A promise that resolves to the signed message as a string.
101101
*/
102102
async signMessage(message: string): Promise<string> {
103-
if (!this.connected) throw new Error("Not connected");
104-
if (!this.accessKey) throw new Error("Access key undefined");
103+
if (!this.connected) throw new Error(ErrorCodes.NOT_CONNECTED);
104+
if (!this.accessKey) throw new Error(ErrorCodes.NO_ACCESS_KEY);
105105
return this.provider.signMessage(this.accessKey, message);
106106
}
107107

108108
/**
109109
* Executes any pending transactions stored in the pending transaction store.
110110
*/
111111
async executePendingTransactions(): Promise<void> {
112-
if (!this.connected) throw new Error("Not connected");
112+
if (!this.connected) throw new Error(ErrorCodes.NOT_CONNECTED);
113113
if (!this.pendingTransactionStore) return;
114114
let pendingTransaction: Transaction | null = await this.pendingTransactionStore.pop(this.account!.accountId);
115115
while (pendingTransaction) {
116-
if (!this.account?.accessKey) throw new Error("Access key undefined");
116+
if (!this.account?.accessKey) throw new Error(ErrorCodes.NO_ACCESS_KEY);
117117
// NOTE: We wait for certain seconds to make sure transaction gets included in the blockchain
118118
await new Promise((resolve) => setTimeout(resolve, 1500));
119119
await this.signAndSendTransaction(pendingTransaction);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export enum ErrorCodes {
2+
NOT_CONNECTED = "Client is not connected",
3+
NO_ACCESS_KEY = "No access key provided or stored",
4+
COULD_NOT_CONNECT = "Could not sign in using One Click Connect, no msgSignIn detected and no account stored",
5+
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
export * from "./repository.errors";
2-
export * from "./service.errors";
1+
export * from "./error-codes";

packages/dapp/core/src/common/errors/repository.errors.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/dapp/core/src/common/errors/service.errors.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export enum ErrorCodes {
2+
METHOD_NOT_IMPLEMENTED = "Method not implemented",
3+
UNREACHABLE = "This code should not be reachable",
4+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./error-codes";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
export * from "./errors";
12
export { setupOneClickConnect } from "./setup";

packages/dapp/wallet-selector/src/wallet.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { SignAndSendTransactionParams, SignAndSendTransactionsParams } from "./t
1212
import { FunctionCallPermission } from "near-api-js/lib/transaction";
1313
import { DAppClient } from "@one-click-connect/dapp-sdk";
1414
import { createAction } from "@near-wallet-selector/wallet-utils";
15+
import { ErrorCodes } from "./errors";
1516

1617
export class Wallet implements Omit<InstantLinkWallet, "id" | "type" | "metadata"> {
1718
constructor(
@@ -49,7 +50,7 @@ export class Wallet implements Omit<InstantLinkWallet, "id" | "type" | "metadata
4950
* @inheritDoc
5051
*/
5152
verifyOwner = (_: VerifyOwnerParams): Promise<VerifiedOwner | void> => {
52-
throw new Error("Method not implemented.");
53+
throw new Error(ErrorCodes.METHOD_NOT_IMPLEMENTED);
5354
};
5455

5556
/**
@@ -62,7 +63,7 @@ export class Wallet implements Omit<InstantLinkWallet, "id" | "type" | "metadata
6263
receiverId: params.receiverId ?? this.permissions.receiverId,
6364
actions,
6465
});
65-
if (!result) throw new Error("unreachable code");
66+
if (!result) throw new Error(ErrorCodes.UNREACHABLE);
6667
return result;
6768
};
6869

packages/wallet/core/src/client/wallet.client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export class WalletClient<Transaction, Config extends WalletClientConfig = Walle
5858
},
5959
};
6060
} catch (_) {}
61-
throw new Error("Invalid URL");
61+
throw new Error(ClientErrorCodes.INVALID_URL);
6262
}
6363

6464
/**

0 commit comments

Comments
 (0)