Skip to content

Commit

Permalink
add error classes
Browse files Browse the repository at this point in the history
  • Loading branch information
baymac committed May 20, 2024
1 parent dde052e commit e9767ab
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 7 deletions.
65 changes: 65 additions & 0 deletions packages/wc-dapp/src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
export class ProviderNotInitialisedError extends Error {
error: unknown;

constructor(errorMsg: string) {
// 'Error' breaks prototype chain here
super(errorMsg);
// Set the prototype explicitly.
Object.setPrototypeOf(this, ProviderNotInitialisedError.prototype);
}
}

export class EnabledApiNotFoundError extends Error {
error: unknown;

constructor(errorMsg: string) {
// 'Error' breaks prototype chain here
super(errorMsg);
// Set the prototype explicitly.
Object.setPrototypeOf(this, EnabledApiNotFoundError.prototype);
}
}

export class DefaultChainNotSetError extends Error {
error: unknown;

constructor(errorMsg: string) {
// 'Error' breaks prototype chain here
super(errorMsg);
// Set the prototype explicitly.
Object.setPrototypeOf(this, DefaultChainNotSetError.prototype);
}
}

export class AccountNotSetError extends Error {
error: unknown;

constructor(errorMsg: string) {
// 'Error' breaks prototype chain here
super(errorMsg);
// Set the prototype explicitly.
Object.setPrototypeOf(this, AccountNotSetError.prototype);
}
}

export class ConnectionAbortedByUserError extends Error {
error: unknown;

constructor(errorMsg: string) {
// 'Error' breaks prototype chain here
super(errorMsg);
// Set the prototype explicitly.
Object.setPrototypeOf(this, ConnectionAbortedByUserError.prototype);
}
}

export class Web3ModalInitError extends Error {
error: unknown;

constructor(errorMsg: string) {
// 'Error' breaks prototype chain here
super(errorMsg);
// Set the prototype explicitly.
Object.setPrototypeOf(this, Web3ModalInitError.prototype);
}
}
1 change: 1 addition & 0 deletions packages/wc-dapp/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './constants';
export * from './errors';
export * from './provider';
export * from './types';
23 changes: 16 additions & 7 deletions packages/wc-dapp/src/provider/cardanoProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ import { SessionTypes, SignClientTypes } from '@walletconnect/types';
import UniversalProvider, { ConnectParams } from '@walletconnect/universal-provider';

import { ACCOUNT, DEFAULT_LOGGER, STORAGE, SUPPORTED_EXPLORER_WALLETS } from '../constants';
import {
AccountNotSetError,
ConnectionAbortedByUserError,
DefaultChainNotSetError,
EnabledApiNotFoundError,
ProviderNotInitialisedError,
Web3ModalInitError
} from '../errors';
import { CardanoProviderOpts, TRpc } from '../types';
import { EnabledAPI } from './enabledApi';
import {
Expand Down Expand Up @@ -68,8 +76,8 @@ export class CardanoProvider {
} else {
await this.loadPersistedSession(sam);
}
if (!this.provider) throw new Error('Provider not initialized');
if (!this.enabledApi) throw new Error('Enabled API not initialized');
if (!this.provider) throw new ProviderNotInitialisedError('Provider not initialized');
if (!this.enabledApi) throw new EnabledApiNotFoundError('Enabled API not initialized');
return this.enabledApi;
}

Expand All @@ -78,7 +86,7 @@ export class CardanoProvider {
const chainId =
provider.namespaces?.[CARDANO_NAMESPACE_NAME].defaultChain ||
provider.namespaces?.[CARDANO_NAMESPACE_NAME].chains[0].split(':')[1];
if (!chainId) throw new Error('Default chain not set');
if (!chainId) throw new DefaultChainNotSetError('Default chain not set');
return chainId;
}

Expand All @@ -88,12 +96,13 @@ export class CardanoProvider {
(await this.getFromStore(ACCOUNT)) ||
provider.session?.namespaces?.[CARDANO_NAMESPACE_NAME].accounts[0];

if (!storedAccount) throw new Error('No account set');
if (!storedAccount) throw new AccountNotSetError('No account set');
return storedAccount;
}

getProvider(): UniversalProvider {
if (!this.provider) throw new Error('Provider not initialized. Call init() first');
if (!this.provider)
throw new ProviderNotInitialisedError('Provider not initialized. Call init() first');
return this.provider;
}

Expand Down Expand Up @@ -152,7 +161,7 @@ export class CardanoProvider {
provider.abortPairingAttempt();
await provider.cleanupPendingPairings({ deletePairings: true });
this.reset();
reject(new Error('Connection aborted by user.'));
reject(new ConnectionAbortedByUserError('Connection aborted by user.'));
}
});
provider
Expand Down Expand Up @@ -273,6 +282,6 @@ export const getWeb3Modal = (projectId: string, chains: CHAIN[]) => {
explorerExcludedWalletIds: 'ALL'
});
} catch (e) {
throw new Error(`Error instantiating web3Modal: ${JSON.stringify(e)}`);
throw new Web3ModalInitError(`Error instantiating web3Modal: ${JSON.stringify(e)}`);
}
};

0 comments on commit e9767ab

Please sign in to comment.