Skip to main content

bittensor_core/
error.rs

1//! The single error type crossing the core's boundary.
2//!
3//! Binding crates map each variant onto their language's existing exception
4//! taxonomy exactly once (for Python: `KeyfileError`, `WrongPasswordError`,
5//! `StorageFunctionNotFound`, `ValueError`), so no caller above the binding
6//! ever changes an except clause.
7
8use core::fmt;
9
10#[derive(Debug)]
11pub enum CoreError {
12    /// Keyfile data is malformed or uses an unknown encryption envelope.
13    Keyfile(String),
14    /// Keyfile decryption failed because the password is wrong.
15    WrongPassword(String),
16    /// A named pallet / storage item / call / runtime API does not exist in
17    /// the runtime this operation was given.
18    NotInRuntime(String),
19    /// Encode/decode rejected the value or bytes (maps to ValueError).
20    Codec(String),
21    /// Crypto operation failed (bad key length, invalid signature bytes...).
22    Crypto(String),
23    /// A hardware signing device could not be reached, rejected the request,
24    /// or the user declined on-device.
25    Device(String),
26    /// JSON-RPC transport, protocol, or receipt processing failed.
27    Rpc(String),
28    /// A semantic transaction was rejected before signing by local policy.
29    Policy(String),
30}
31
32impl fmt::Display for CoreError {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        match self {
35            CoreError::Keyfile(msg) => write!(f, "keyfile error: {msg}"),
36            CoreError::WrongPassword(msg) => write!(f, "{msg}"),
37            CoreError::NotInRuntime(what) => write!(f, "{what} not found in this runtime"),
38            CoreError::Codec(msg) => write!(f, "codec error: {msg}"),
39            CoreError::Crypto(msg) => write!(f, "crypto error: {msg}"),
40            CoreError::Device(msg) => write!(f, "device error: {msg}"),
41            CoreError::Rpc(msg) => write!(f, "rpc error: {msg}"),
42            CoreError::Policy(msg) => write!(f, "policy error: {msg}"),
43        }
44    }
45}
46
47impl std::error::Error for CoreError {}