casper_client/
error.rs

1use std::{io, path::PathBuf};
2
3use thiserror::Error;
4
5use casper_types::{bytesrepr::Error as ToBytesError, crypto, Key};
6#[cfg(doc)]
7use casper_types::{CLValue, URef};
8
9#[cfg(doc)]
10use crate::types::{Deploy, DeployBuilder, TimeDiff, Timestamp};
11use crate::{validation::ValidateResponseError, JsonRpcId};
12
13/// Errors that may be returned by `casper_client` functions.
14#[derive(Error, Debug)]
15pub enum Error {
16    /// [`Deploy`] size too large.
17    #[error("deploy size of {actual_deploy_size} bytes exceeds limit of {max_deploy_size}")]
18    DeploySizeTooLarge {
19        /// The maximum permitted serialized deploy size, in bytes.
20        max_deploy_size: u32,
21        /// The serialized size of the deploy provided, in bytes.
22        actual_deploy_size: usize,
23    },
24
25    /// Failed to build [`Deploy`] due to missing payment code.
26    ///
27    /// Call [`DeployBuilder::with_standard_payment`] or [`DeployBuilder::with_payment`] before
28    /// calling [`DeployBuilder::build`].
29    #[error("deploy requires payment code - use `with_payment` or `with_standard_payment`")]
30    DeployMissingPaymentCode,
31
32    /// Invalid [`Key`] variant.
33    #[error("expected {} but got {}", .expected_variant, .actual)]
34    InvalidKeyVariant {
35        /// The expected variant.
36        expected_variant: String,
37        /// The actual key provided.
38        actual: Key,
39    },
40
41    /// Failed to get a response from the node.
42    #[error("failed to get response for rpc-id {rpc_id} {rpc_method}: {error}")]
43    FailedToGetResponse {
44        /// The JSON-RPC ID.
45        rpc_id: JsonRpcId,
46        /// The JSON-RPC request method.
47        rpc_method: &'static str,
48        /// The reported error.
49        error: reqwest::Error,
50    },
51
52    /// JSON-RPC error returned from the node.
53    #[error("response for rpc-id {rpc_id} {rpc_method} is http error: {error}")]
54    ResponseIsHttpError {
55        /// The JSON-RPC ID.
56        rpc_id: JsonRpcId,
57        /// The JSON-RPC request method.
58        rpc_method: &'static str,
59        /// The reported error.
60        error: reqwest::Error,
61    },
62
63    /// Failed to parse the response.
64    #[error("failed to parse response for rpc-id {rpc_id} {rpc_method}: {error}")]
65    FailedToParseResponse {
66        /// The JSON-RPC ID.
67        rpc_id: JsonRpcId,
68        /// The JSON-RPC request method.
69        rpc_method: &'static str,
70        /// The reported error.
71        error: reqwest::Error,
72    },
73
74    /// JSON-RPC error returned from the node.
75    #[error("response for rpc-id {rpc_id} {rpc_method} is json-rpc error: {error}")]
76    ResponseIsRpcError {
77        /// The JSON-RPC ID.
78        rpc_id: JsonRpcId,
79        /// The JSON-RPC request method.
80        rpc_method: &'static str,
81        /// The reported error.
82        error: jsonrpc_lite::Error,
83    },
84
85    /// Invalid response returned from the node.
86    #[error(
87        "response {response_kind} for rpc-id {rpc_id} {rpc_method} is not valid because {source:?}: {response}"
88    )]
89    InvalidRpcResponse {
90        /// The JSON-RPC ID.
91        rpc_id: JsonRpcId,
92        /// The JSON-RPC request method.
93        rpc_method: &'static str,
94        /// What kind of Json response was received.
95        response_kind: &'static str,
96        /// The JSON response.
97        response: serde_json::Value,
98        /// If available, the original error from Serde.
99        source: Option<serde_json::Error>,
100    },
101
102    /// Failed to encode to JSON.
103    #[error("failed to encode to json: {context}: {error}")]
104    FailedToEncodeToJson {
105        /// Contextual description of where this error occurred.
106        context: &'static str,
107        /// Underlying encoding error.
108        error: serde_json::Error,
109    },
110
111    /// Failed to decode from JSON.
112    #[error("failed to decode from json: {context}: {error}")]
113    FailedToDecodeFromJson {
114        /// Contextual description of where this error occurred.
115        context: &'static str,
116        /// Underlying decoding error.
117        error: serde_json::Error,
118    },
119
120    /// Failed to create new file because it already exists.
121    #[error("file at {} already exists", .0.display())]
122    FileAlreadyExists(PathBuf),
123
124    /// Empty path provided as output dir for keygen.
125    #[error("empty path provided as output dir for keygen")]
126    EmptyKeygenPath,
127
128    /// Unsupported keygen algorithm.
129    #[error("unsupported keygen algorithm: {0}")]
130    UnsupportedAlgorithm(String),
131
132    /// Context-adding wrapper for `std::io::Error`.
133    #[error("input/output error: {context}: {error}")]
134    IoError {
135        /// Contextual description of where this error occurred including relevant paths,
136        /// filenames, etc.
137        context: String,
138        /// std::io::Error raised during the operation in question.
139        error: io::Error,
140    },
141
142    /// Failed to serialize to bytes.
143    #[error("serialization error: {0}")]
144    ToBytesError(ToBytesError),
145
146    /// Cryptographic error.
147    #[error("cryptographic error: {context}: {error}")]
148    CryptoError {
149        /// Contextual description of where this error occurred including relevant paths,
150        /// filenames, etc.
151        context: &'static str,
152        /// Underlying crypto error.
153        error: crypto::ErrorExt,
154    },
155
156    /// Failed to validate response.
157    #[error("invalid response: {0}")]
158    ResponseFailedValidation(#[from] ValidateResponseError),
159}
160
161impl From<ToBytesError> for Error {
162    fn from(error: ToBytesError) -> Self {
163        Error::ToBytesError(error)
164    }
165}