Skip to main content

solana_wasi/
error.rs

1//! One error type for the whole crate.
2//!
3//! Deliberately hand-rolled rather than `thiserror`-derived: the dependency
4//! footprint of a WIT component is the thing an operator audits, and this saves
5//! a proc-macro crate for about forty lines of `Display`.
6
7use core::fmt;
8
9/// Everything that can go wrong inside `solana-wasi`.
10#[derive(Debug, Clone, PartialEq, Eq)]
11pub enum Error {
12    /// A base58 string was not a valid 32-byte public key.
13    InvalidPubkey(String),
14    /// An account's data did not match the layout it was parsed as.
15    InvalidAccountData(String),
16    /// The transport refused or failed the request.
17    Transport(String),
18    /// The node answered with a JSON-RPC `error` member.
19    Rpc {
20        /// JSON-RPC error code.
21        code: i64,
22        /// Server-supplied message, truncated to 200 characters.
23        message: String,
24    },
25    /// The node answered with JSON that did not match the expected shape.
26    UnexpectedResponse(String),
27    /// A requested account does not exist on the cluster.
28    AccountNotFound(String),
29    /// Seeds could not be turned into a program address.
30    InvalidSeeds(&'static str),
31    /// A caller-supplied value was rejected before any network call.
32    InvalidArgument(String),
33    /// A transaction could not be encoded (too many accounts, oversized, ...).
34    Encode(String),
35}
36
37impl fmt::Display for Error {
38    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39        match self {
40            Error::InvalidPubkey(s) => write!(f, "invalid pubkey: {s}"),
41            Error::InvalidAccountData(s) => write!(f, "invalid account data: {s}"),
42            Error::Transport(s) => write!(f, "transport error: {s}"),
43            Error::Rpc { code, message } => write!(f, "rpc error {code}: {message}"),
44            Error::UnexpectedResponse(s) => write!(f, "unexpected rpc response: {s}"),
45            Error::AccountNotFound(s) => write!(f, "account not found: {s}"),
46            Error::InvalidSeeds(s) => write!(f, "invalid seeds: {s}"),
47            Error::InvalidArgument(s) => write!(f, "invalid argument: {s}"),
48            Error::Encode(s) => write!(f, "encode error: {s}"),
49        }
50    }
51}
52
53impl std::error::Error for Error {}
54
55/// Crate-wide result alias.
56pub type Result<T> = core::result::Result<T, Error>;