From 1e14d114d7c4974331baa8d6cf1b1140ac743178 Mon Sep 17 00:00:00 2001 From: Yuru Shao Date: Mon, 20 Jan 2025 11:20:31 -0800 Subject: [PATCH] cli: improve error messages (#373) --- anchor/src/client/base.ts | 2 +- cli/src/main.ts | 56 ++++++++++++++++++++++----------------- cli/src/utils.ts | 18 ++++++++++++- 3 files changed, 49 insertions(+), 27 deletions(-) diff --git a/anchor/src/client/base.ts b/anchor/src/client/base.ts index 1b507638..7d4ade2d 100644 --- a/anchor/src/client/base.ts +++ b/anchor/src/client/base.ts @@ -203,7 +203,7 @@ export class BaseClient { lookupTables, ); } catch (e) { - console.error(e); + // TODO: add a flag to control if we should throw error on failed simulation // ignore // when we run tests with failure cases, this RPC call fails with // an incorrect error message so we should ignore it diff --git a/cli/src/main.ts b/cli/src/main.ts index cb193303..78f3597e 100644 --- a/cli/src/main.ts +++ b/cli/src/main.ts @@ -7,6 +7,7 @@ import { GlamClient, VaultIntegrations, GlamPermissions, + GlamError, } from "@glam/anchor"; import { LAMPORTS_PER_SOL, PublicKey } from "@solana/web3.js"; import { Command } from "commander"; @@ -14,7 +15,7 @@ import { Command } from "commander"; import fs from "fs"; import inquirer from "inquirer"; -import { loadingConfig, setStateToConfig } from "./utils"; +import { loadingConfig, parseTxError, setStateToConfig } from "./utils"; import { QuoteParams } from "anchor/src/client/jupiter"; import { VersionedTransaction } from "@solana/web3.js"; @@ -173,7 +174,7 @@ program setStateToConfig(statePda.toBase58()); } catch (e) { - console.error(e); + console.error(parseTxError(e)); process.exit(1); } }); @@ -235,7 +236,7 @@ program ); setStateToConfig(null); } catch (e) { - console.error(e); + console.error(parseTxError(e)); process.exit(1); } }); @@ -275,13 +276,18 @@ program } } - const txSig = await glamClient.state.withdraw( - statePda, - new PublicKey(asset), - new anchor.BN(parseFloat(amount) * 10 ** mint.decimals), - txOptions, - ); - console.log(`Withdrawn ${amount} ${asset}:`, txSig); + try { + const txSig = await glamClient.state.withdraw( + statePda, + new PublicKey(asset), + new anchor.BN(parseFloat(amount) * 10 ** mint.decimals), + txOptions, + ); + console.log(`Withdrawn ${amount} ${asset}:`, txSig); + } catch (e) { + console.error(parseTxError(e)); + process.exit(1); + } }); const delegate = program.command("delegate").description("Manage delegates"); @@ -352,7 +358,7 @@ delegate console.log("txSig:", txSig); console.log(`Granted ${pubkey} permissions ${permissions}`); } catch (e) { - console.error(e); + console.error(parseTxError(e)); process.exit(1); } }); @@ -377,7 +383,7 @@ delegate console.log("txSig:", txSig); console.log(`Revoked ${pubkey} access to ${statePda.toBase58()}`); } catch (e) { - console.error(e); + console.error(parseTxError(e)); process.exit(1); } }); @@ -469,7 +475,7 @@ integration `${integration} enabled on ${stateModel} (${statePda.toBase58()})`, ); } catch (e) { - console.error(e); + console.error(parseTxError(e)); process.exit(1); } }); @@ -509,7 +515,7 @@ integration `${integration} disabled on ${stateModel.name} (${statePda.toBase58()})`, ); } catch (e) { - console.error(e); + console.error(parseTxError(e)); process.exit(1); } }); @@ -536,7 +542,7 @@ jup console.log("txSig", txSig); console.log(`Staked ${amount} JUP`); } catch (e) { - console.error(e); + console.error(parseTxError(e)); throw e; } }); @@ -559,7 +565,7 @@ jup console.log("txSig", txSig); console.log("Unstaked all JUP tokens"); } catch (e) { - console.error(e); + console.error(parseTxError(e)); throw e; } }); @@ -597,9 +603,9 @@ const vote = program governor, Number(side), ); - console.log("castVote txId", txId); + console.log("castVote:", txId); } catch (e) { - console.error(e); + console.error(parseTxError(e)); throw e; } }); @@ -627,7 +633,7 @@ program const txSig = await glamClient.wsol.wrap(statePda, lamports, txOptions); console.log(`Wrapped ${amount} SOL:`, txSig); } catch (e) { - console.error(e); + console.error(parseTxError(e)); process.exit(1); } }); @@ -649,7 +655,7 @@ program const txSig = await glamClient.wsol.unwrap(statePda, txOptions); console.log(`All wSOL unwrapped:`, txSig); } catch (e) { - console.error(e); + console.error(parseTxError(e)); process.exit(1); } }); @@ -765,7 +771,7 @@ program ); console.log(`Swapped ${amount} ${from} to ${to}`); } catch (e) { - console.error(e); + console.error(parseTxError(e)); process.exit(1); } }); @@ -795,7 +801,7 @@ lst console.log("txSig", txSig); console.log(`Staked ${amount} SOL into ${stakepool}`); } catch (e) { - console.error(e); + console.error(parseTxError(e)); process.exit(1); } }); @@ -822,7 +828,7 @@ lst ); console.log(`Unstaked ${amount} ${asset}:`, txSig); } catch (e) { - console.error(e); + console.error(parseTxError(e)); process.exit(1); } }); @@ -884,7 +890,7 @@ lst ); console.log(`Withdrew from ${accounts}:`, txSig); } catch (e) { - console.error(e); + console.error(parseTxError(e)); process.exit(1); } }); @@ -944,7 +950,7 @@ lst ); console.log(`Claimed ${tickets}:`, txSig); } catch (e) { - console.error(e); + console.error(parseTxError(e)); process.exit(1); } }); diff --git a/cli/src/utils.ts b/cli/src/utils.ts index c88f9551..8d0ea44f 100644 --- a/cli/src/utils.ts +++ b/cli/src/utils.ts @@ -1,5 +1,9 @@ +import { AnchorError } from "@coral-xyz/anchor"; import { PriorityLevel } from "@glam/anchor"; -import { PublicKey } from "@solana/web3.js"; +import { + PublicKey, + TransactionExpiredBlockheightExceededError, +} from "@solana/web3.js"; import fs from "fs"; import os from "os"; import path from "path"; @@ -67,3 +71,15 @@ export const setStateToConfig = (statePda: string) => { const updated = { ...JSON.parse(config), glam_state: statePda }; fs.writeFileSync(configPath, JSON.stringify(updated, null, 2), "utf8"); }; + +export const parseTxError = (error: any) => { + if (error instanceof TransactionExpiredBlockheightExceededError) { + return "Transaction expired"; + } + + if (error instanceof AnchorError) { + return error.error.errorMessage; + } + + return error?.message || "Unknown error"; +};