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, Deploy, TimeDiff, Timestamp, URef};
8
9use crate::{
10    cli::{DeployBuilderError, TransactionV1BuilderError},
11    validation::ValidateResponseError,
12    JsonRpcId,
13};
14
15/// Errors that may be returned by `casper_client` functions.
16#[derive(Error, Debug)]
17pub enum Error {
18    /// [`Deploy`] size too large.
19    #[error(transparent)]
20    DeploySize(#[from] casper_types::DeployExcessiveSizeError),
21
22    /// Failed to build [`Deploy`].
23    #[error(transparent)]
24    DeployBuild(#[from] DeployBuilderError),
25
26    /// Failed to build Transaction
27    #[error(transparent)]
28    TransactionBuild(#[from] TransactionV1BuilderError),
29    /// Invalid [`Key`] variant.
30    #[error("expected {} but got {}", .expected_variant, .actual)]
31    InvalidKeyVariant {
32        /// The expected variant.
33        expected_variant: String,
34        /// The actual key provided.
35        actual: Key,
36    },
37
38    /// Failed to get a response from the node.
39    #[error("failed to get response for rpc-id {rpc_id} {rpc_method}: {error}")]
40    FailedToGetResponse {
41        /// The JSON-RPC ID.
42        rpc_id: JsonRpcId,
43        /// The JSON-RPC request method.
44        rpc_method: &'static str,
45        /// The reported error.
46        error: reqwest::Error,
47    },
48
49    /// JSON-RPC error returned from the node.
50    #[error("response for rpc-id {rpc_id} {rpc_method} is http error: {error}")]
51    ResponseIsHttpError {
52        /// The JSON-RPC ID.
53        rpc_id: JsonRpcId,
54        /// The JSON-RPC request method.
55        rpc_method: &'static str,
56        /// The reported error.
57        error: reqwest::Error,
58    },
59
60    /// Failed to parse the response.
61    #[error("failed to parse response for rpc-id {rpc_id} {rpc_method}: {error}")]
62    FailedToParseResponse {
63        /// The JSON-RPC ID.
64        rpc_id: JsonRpcId,
65        /// The JSON-RPC request method.
66        rpc_method: &'static str,
67        /// The reported error.
68        error: reqwest::Error,
69    },
70
71    /// JSON-RPC error returned from the node.
72    #[error("response for rpc-id {rpc_id} {rpc_method} is json-rpc error: {error}")]
73    ResponseIsRpcError {
74        /// The JSON-RPC ID.
75        rpc_id: JsonRpcId,
76        /// The JSON-RPC request method.
77        rpc_method: &'static str,
78        /// The reported error.
79        error: jsonrpc_lite::Error,
80    },
81
82    /// Invalid response returned from the node.
83    #[error(
84        "response {response_kind} for rpc-id {rpc_id} {rpc_method} is not valid because {source:?}: {response}"
85    )]
86    InvalidRpcResponse {
87        /// The JSON-RPC ID.
88        rpc_id: JsonRpcId,
89        /// The JSON-RPC request method.
90        rpc_method: &'static str,
91        /// What kind of Json response was received.
92        response_kind: &'static str,
93        /// The JSON response.
94        response: serde_json::Value,
95        /// If available, the original error from Serde.
96        source: Option<serde_json::Error>,
97    },
98
99    /// Failed to encode to JSON.
100    #[error("failed to encode to json: {context}: {error}")]
101    FailedToEncodeToJson {
102        /// Contextual description of where this error occurred.
103        context: &'static str,
104        /// Underlying encoding error.
105        error: serde_json::Error,
106    },
107
108    /// Failed to decode from JSON.
109    #[error("failed to decode from json: {context}: {error}")]
110    FailedToDecodeFromJson {
111        /// Contextual description of where this error occurred.
112        context: &'static str,
113        /// Underlying decoding error.
114        error: serde_json::Error,
115    },
116
117    /// Failed to create new file because it already exists.
118    #[error("file at {} already exists", .0.display())]
119    FileAlreadyExists(PathBuf),
120
121    /// Empty path provided as output dir for keygen.
122    #[error("empty path provided as output dir for keygen")]
123    EmptyKeygenPath,
124
125    /// Unsupported keygen algorithm.
126    #[error("unsupported keygen algorithm: {0}")]
127    UnsupportedAlgorithm(String),
128
129    /// Context-adding wrapper for `std::io::Error`.
130    #[error("input/output error: {context}: {error}")]
131    IoError {
132        /// Contextual description of where this error occurred including relevant paths,
133        /// filenames, etc.
134        context: String,
135        /// std::io::Error raised during the operation in question.
136        error: io::Error,
137    },
138
139    /// Failed to serialize to bytes.
140    #[error("serialization error: {0}")]
141    ToBytesError(ToBytesError),
142
143    /// Cryptographic error.
144    #[error("cryptographic error: {context}: {error}")]
145    CryptoError {
146        /// Contextual description of where this error occurred including relevant paths,
147        /// filenames, etc.
148        context: &'static str,
149        /// Underlying crypto error.
150        error: crypto::ErrorExt,
151    },
152
153    /// Failed to validate response.
154    #[error("invalid response: {0}")]
155    ResponseFailedValidation(#[from] ValidateResponseError),
156
157    /// An error that occurs when attempting to use a non UTF-8 string for a transaction.
158    #[error("invalid UTF-8 string: {context}: {error}")]
159    Utf8Error {
160        /// Contextual description of where this error occurred.
161        context: &'static str,
162        /// Underlying error.
163        error: std::str::Utf8Error,
164    },
165
166    /// Failed to verify contract.
167    #[error("contract verification failed")]
168    ContractVerificationFailed,
169
170    /// Failed to construct HTTP client.
171    #[error("failed to construct HTTP client")]
172    FailedToConstructHttpClient,
173
174    /// Failed to parse provided node address as URL.
175    #[error("failed to parse node address as valid URL")]
176    FailedToParseNodeAddress,
177}
178
179impl From<ToBytesError> for Error {
180    fn from(error: ToBytesError) -> Self {
181        Error::ToBytesError(error)
182    }
183}