Skip to content

Commit

Permalink
cli: improve error messages (#373)
Browse files Browse the repository at this point in the history
  • Loading branch information
yurushao authored Jan 20, 2025
1 parent 8789706 commit 1e14d11
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 27 deletions.
2 changes: 1 addition & 1 deletion anchor/src/client/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 31 additions & 25 deletions cli/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ import {
GlamClient,
VaultIntegrations,
GlamPermissions,
GlamError,
} from "@glam/anchor";
import { LAMPORTS_PER_SOL, PublicKey } from "@solana/web3.js";
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";

Expand Down Expand Up @@ -173,7 +174,7 @@ program

setStateToConfig(statePda.toBase58());
} catch (e) {
console.error(e);
console.error(parseTxError(e));
process.exit(1);
}
});
Expand Down Expand Up @@ -235,7 +236,7 @@ program
);
setStateToConfig(null);
} catch (e) {
console.error(e);
console.error(parseTxError(e));
process.exit(1);
}
});
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
}
});
Expand All @@ -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);
}
});
Expand Down Expand Up @@ -469,7 +475,7 @@ integration
`${integration} enabled on ${stateModel} (${statePda.toBase58()})`,
);
} catch (e) {
console.error(e);
console.error(parseTxError(e));
process.exit(1);
}
});
Expand Down Expand Up @@ -509,7 +515,7 @@ integration
`${integration} disabled on ${stateModel.name} (${statePda.toBase58()})`,
);
} catch (e) {
console.error(e);
console.error(parseTxError(e));
process.exit(1);
}
});
Expand All @@ -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;
}
});
Expand All @@ -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;
}
});
Expand Down Expand Up @@ -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;
}
});
Expand Down Expand Up @@ -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);
}
});
Expand All @@ -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);
}
});
Expand Down Expand Up @@ -765,7 +771,7 @@ program
);
console.log(`Swapped ${amount} ${from} to ${to}`);
} catch (e) {
console.error(e);
console.error(parseTxError(e));
process.exit(1);
}
});
Expand Down Expand Up @@ -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);
}
});
Expand All @@ -822,7 +828,7 @@ lst
);
console.log(`Unstaked ${amount} ${asset}:`, txSig);
} catch (e) {
console.error(e);
console.error(parseTxError(e));
process.exit(1);
}
});
Expand Down Expand Up @@ -884,7 +890,7 @@ lst
);
console.log(`Withdrew from ${accounts}:`, txSig);
} catch (e) {
console.error(e);
console.error(parseTxError(e));
process.exit(1);
}
});
Expand Down Expand Up @@ -944,7 +950,7 @@ lst
);
console.log(`Claimed ${tickets}:`, txSig);
} catch (e) {
console.error(e);
console.error(parseTxError(e));
process.exit(1);
}
});
Expand Down
18 changes: 17 additions & 1 deletion cli/src/utils.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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";
};

0 comments on commit 1e14d11

Please sign in to comment.