diff --git a/packages/core/src/contract/deploy-contract.ts b/packages/core/src/contract/deploy-contract.ts index 30534f7..752f149 100644 --- a/packages/core/src/contract/deploy-contract.ts +++ b/packages/core/src/contract/deploy-contract.ts @@ -68,6 +68,6 @@ export async function deployContract ( } catch (error) { console.error('err', error); // TODO: add more error handlers for different scenarios - return returnError('UserRejectedTransactionError'); + return returnError('UserRejectedTransactionError', error as Error); } } diff --git a/packages/core/src/contract/read-contract.ts b/packages/core/src/contract/read-contract.ts index 6284240..9fec28d 100644 --- a/packages/core/src/contract/read-contract.ts +++ b/packages/core/src/contract/read-contract.ts @@ -41,9 +41,9 @@ export async function readContract ); } catch (error) { - return returnError('TonReadError'); + return returnError('TonReadError', error); } } diff --git a/packages/core/src/contract/write-contract.ts b/packages/core/src/contract/write-contract.ts index cf48c0b..e919257 100644 --- a/packages/core/src/contract/write-contract.ts +++ b/packages/core/src/contract/write-contract.ts @@ -53,7 +53,7 @@ export async function writeContract ( +export const returnError = ( type: TYPE, + cause?: unknown ): DataOrError => { - const error = new supportedErrors[type](); + const nativeTonErrors: SupportedErrorsKeys[] = ['TonConnectError', 'TonConnectUIError'] as const; + + const errorClass = supportedErrors[type] + + if (nativeTonErrors.includes(type)) { + const error = new errorClass(); + + return { data: undefined, error: error as SupportedErrors[TYPE] }; + } + + const error = new errorClass(cause as any); return { data: undefined, error: error as SupportedErrors[TYPE] }; }; diff --git a/packages/core/src/shared/errors/syntax-errors.ts b/packages/core/src/shared/errors/syntax-errors.ts index cee98bb..7567d20 100644 --- a/packages/core/src/shared/errors/syntax-errors.ts +++ b/packages/core/src/shared/errors/syntax-errors.ts @@ -6,44 +6,50 @@ export class SyntaxError extends Error { } export class ConnectFunctionUnavailableError extends SyntaxError { - constructor() { + constructor(cause?: unknown) { super('This function is not available for UI-based wallet connections. Use `createWalletClientUI` instead'); this.name = 'ConnectFunctionUnavailableError'; + this.cause = cause; } } export class ConnectUIFunctionUnavailableError extends SyntaxError { - constructor() { + constructor(cause?: unknown) { super('This function is only available for UI-based wallet connections'); this.name = 'ConnectUIFunctionUnavailableError'; + this.cause = cause; } } export class ConnectUIFunctionUnavailableInNodeError extends SyntaxError { - constructor() { + constructor(cause?: unknown) { super('This function is not available in the server environment. Please you `createWalletClient` instead or, if you use Next.js, call `createWalletClientUI` in `useEffect` hook or in the event handler, so it creates a wallet client when the `window` is available.'); this.name = 'ConnectUIFunctionUnavailableInNodeError'; + this.cause = cause; } } export class ReconnectFunctionUnavailableError extends SyntaxError { - constructor() { + constructor(cause?: unknown) { super('The reconnect is not available for UI-based wallet connections. Pass `restoreConnection: true` to the `createWalletClientUI` function to enable it'); this.name = 'ReconnectFunctionUnavailableError'; + this.cause = cause; } } export class IncorrectContractError extends SyntaxError { - constructor() { + constructor(cause?: unknown) { super('The contract is incorrect. Provide the contract class compiled from your Tact or Func files'); this.name = 'IncorrectContractError'; + this.cause = cause; } } export class MissingContractAddressError extends SyntaxError { - constructor() { + constructor(cause?: unknown) { super('The contract address is not provided to the walletClient. Call `setAddress` first'); this.name = 'MissingContractAddressError'; + this.cause = cause; } } diff --git a/packages/core/src/shared/errors/ton-errors.ts b/packages/core/src/shared/errors/ton-errors.ts index 746f025..c180d98 100644 --- a/packages/core/src/shared/errors/ton-errors.ts +++ b/packages/core/src/shared/errors/ton-errors.ts @@ -16,30 +16,34 @@ export class TonError extends Error { } export class TonWalletConnectionError extends TonError { - constructor() { + constructor(cause?: unknown) { super('Wallet connection failed'); this.name = 'TonWalletConnectionError'; + this.cause = cause; } } export class TonWalletDisconnectError extends TonError { - constructor() { + constructor(cause?: unknown) { super('Could not disconnect from the wallet'); this.name = 'TonWalletDisconnectError'; + this.cause = cause; } } export class TonReadError extends TonError { - constructor() { + constructor(cause?: unknown) { super('Cannot process the read request. Either the request returned a non-zero exit code or the data could not be parsed'); this.name = 'TonReadError'; + this.cause = cause; } } export class TonRateLimitError extends TonError { - constructor() { + constructor(cause?: unknown) { super('Rate limit is exceeded. Provide the `authKey` to the public client with a better Ton Center plan to get more requests per second'); this.name = 'TonRateLimitError'; + this.cause = cause; } } diff --git a/packages/core/src/shared/errors/user-errors.ts b/packages/core/src/shared/errors/user-errors.ts index 7f0fe29..f07299a 100644 --- a/packages/core/src/shared/errors/user-errors.ts +++ b/packages/core/src/shared/errors/user-errors.ts @@ -6,22 +6,25 @@ export class UserError extends Error { } export class UserUnauthorizedError extends UserError { - constructor() { + constructor(cause?: unknown) { super('Not authorized. Please, connect the wallet first'); this.name = 'UserUnauthorizedError'; + this.cause = cause; } } export class UserRejectedConnectionError extends UserError { - constructor() { + constructor(cause?: unknown) { super('User rejected the connection to their wallet'); this.name = 'UserRejectedConnectionError'; + this.cause = cause; } } export class UserRejectedTransactionError extends UserError { - constructor() { + constructor(cause?: unknown) { super('User rejected the transaction request from their wallet'); this.name = 'UserRejectedTransactionError'; + this.cause = cause; } } diff --git a/packages/core/src/wallet/send-transaction.ts b/packages/core/src/wallet/send-transaction.ts index a483330..e4e5e6b 100644 --- a/packages/core/src/wallet/send-transaction.ts +++ b/packages/core/src/wallet/send-transaction.ts @@ -32,6 +32,6 @@ export async function sendTransaction ( const hash = bocToHash(res.boc); return returnData(hash); } catch (error) { - return returnError('UserRejectedTransactionError'); + return returnError('UserRejectedTransactionError', error); } }