snops_common/rpc/
error.rs

1use serde::{Deserialize, Serialize};
2use strum_macros::AsRefStr;
3use thiserror::Error;
4
5#[macro_export]
6macro_rules! impl_into_type_str {
7    ($name:path) => {
8        impl From<&$name> for String {
9            fn from(e: &$name) -> Self {
10                e.as_ref().to_string()
11            }
12        }
13    };
14
15    ($name:path, |_| $body:expr) => {
16        impl From<&$name> for String {
17            fn from(_: &$name) -> Self {
18                $body
19            }
20        }
21    };
22
23    ($name:path, |$from_var:ident| $body:expr) => {
24        impl From<&$name> for String {
25            fn from($from_var: &$name) -> Self {
26                use $name::*;
27
28                $body
29            }
30        }
31    };
32}
33
34#[macro_export]
35macro_rules! impl_into_status_code {
36    ($name:path) => {
37        impl From<&$name> for ::http::status::StatusCode {
38            fn from(_: &$name) -> Self {
39                Self::INTERNAL_SERVER_ERROR
40            }
41        }
42    };
43
44    ($name:path, |_| $body:expr) => {
45        impl From<&$name> for ::http::status::StatusCode {
46            fn from(_: &$name) -> Self {
47                $body
48            }
49        }
50    };
51
52    ($name:path, |$from_var:ident| $body:expr) => {
53        impl From<&$name> for ::http::status::StatusCode {
54            fn from($from_var: &$name) -> Self {
55                use $name::*;
56
57                $body
58            }
59        }
60    };
61}
62
63#[derive(Debug, Error, Serialize, Deserialize, AsRefStr)]
64pub enum AgentError {
65    #[error("invalid agent state")]
66    InvalidState,
67    #[error("failed to parse json")]
68    FailedToParseJson,
69    #[error("failed to make a request")]
70    FailedToMakeRequest,
71    #[error("failed to get env info: {0}")]
72    FailedToGetEnvInfo(String),
73    #[error("failed to spawn a process")]
74    FailedToSpawnProcess,
75    #[error("process failed")]
76    ProcessFailed,
77    // TODO @gluax move these errors to a new enum
78    #[error("invalid log level: `{0}`")]
79    InvalidLogLevel(String),
80    #[error("failed to change log level")]
81    FailedToChangeLogLevel,
82    #[error("node client not set")]
83    NodeClientNotSet,
84    #[error("node client not ready")]
85    NodeClientNotReady,
86    #[error("invalid block hash")]
87    InvalidBlockHash,
88    #[error("invalid transaction id")]
89    InvalidTransactionId,
90}
91
92#[derive(Debug, Error, Serialize, Deserialize, AsRefStr)]
93pub enum SnarkosRequestError {
94    #[error("expected agent to be in Node state")]
95    InvalidState,
96    #[error("expected Node to be online")]
97    OfflineNode,
98    #[error("failed to obtain environment info")]
99    MissingEnvInfo,
100    #[error("error making request: {0}")]
101    RequestError(String),
102    #[error("error parsing json: {0}")]
103    JsonParseError(String),
104    #[error("error serializing json: {0}")]
105    JsonSerializeError(String),
106    #[error("error deserializing json: {0}")]
107    JsonDeserializeError(String),
108    #[error("rpc error: {0}")]
109    RpcError(String),
110    #[error("request timed out")]
111    TimedOut,
112}
113
114#[derive(Debug, Error, Serialize, Deserialize, AsRefStr)]
115pub enum ResolveError {
116    #[error("source agent not found")]
117    SourceAgentNotFound,
118    #[error("agent has no addresses")]
119    AgentHasNoAddresses,
120}
121
122#[derive(Debug, Error, Serialize, Deserialize, AsRefStr)]
123pub enum ReconcileError {
124    #[error("aborted by a more recent reconcilation request")]
125    Aborted,
126    #[error("failed setup storage: {0}")]
127    StorageSetupError(String),
128    #[error("failed to download {0} from the control plane")]
129    StorageAcquireError(String),
130    #[error("failed to get the binary from the control plane: {0}")]
131    BinaryAcquireError(String),
132    #[error("failed to find a checkpoint for the requested height/span")]
133    CheckpointAcquireError,
134    #[error("failed to apply checkpoint: {0}")]
135    CheckpointApplyError(String),
136    #[error("failed to resolve addresses of stated peers")]
137    ResolveAddrError(ResolveError),
138    #[error("a rention policy is required to rewind the ledger")]
139    MissingRetentionPolicy,
140    #[error("failed to load checkpoints for storage")]
141    CheckpointLoadError,
142    #[error("agent did not provide a local private key")]
143    NoLocalPrivateKey,
144    #[error("generic database error")]
145    Database,
146    #[error("unknown error")]
147    Unknown,
148}