casper_client/cli/
error.rs

1use std::num::ParseIntError;
2
3use humantime::{DurationError, TimestampError};
4use thiserror::Error;
5
6#[cfg(doc)]
7use casper_types::{account::AccountHash, Key, NamedArg, PublicKey, RuntimeArgs, URef};
8use casper_types::{CLValueError, KeyFromStrError, UIntParseError, URefFromStrError};
9
10use crate::cli::JsonArgsError;
11#[cfg(doc)]
12use crate::{
13    rpcs::{DictionaryItemIdentifier, GlobalStateIdentifier},
14    types::{TimeDiff, Timestamp},
15};
16
17/// Error that can be returned by the `cli` API.
18#[derive(Error, Debug)]
19pub enum CliError {
20    /// Failed to parse a [`Key`] from a formatted string.
21    #[error("failed to parse {context} as a key: {error}")]
22    FailedToParseKey {
23        /// Contextual description of where this error occurred.
24        context: &'static str,
25        /// The actual error raised.
26        error: KeyFromStrError,
27    },
28
29    /// Failed to parse a [`PublicKey`] from a formatted string.
30    #[error("failed to parse {context} as a public key: {error}")]
31    FailedToParsePublicKey {
32        /// Contextual description of where this error occurred.
33        context: String,
34        /// The actual error raised.
35        error: casper_types::crypto::Error,
36    },
37
38    /// Failed to parse an [`AccountHash`] from a formatted string.
39    #[error("failed to parse {context} as an account hash: {error}")]
40    FailedToParseAccountHash {
41        /// Contextual description of where this error occurred.
42        context: &'static str,
43        /// The actual error raised.
44        error: casper_types::account::FromStrError,
45    },
46
47    /// Failed to parse a [`URef`] from a formatted string.
48    #[error("failed to parse '{context}' as a uref: {error}")]
49    FailedToParseURef {
50        /// Contextual description of where this error occurred including relevant paths,
51        /// filenames, etc.
52        context: &'static str,
53        /// The actual error raised.
54        error: URefFromStrError,
55    },
56
57    /// Failed to parse an integer from a string.
58    #[error("failed to parse '{context}' as an integer: {error}")]
59    FailedToParseInt {
60        /// Contextual description of where this error occurred including relevant paths,
61        /// filenames, etc.
62        context: &'static str,
63        /// The actual error raised.
64        error: ParseIntError,
65    },
66
67    /// Failed to parse a [`TimeDiff`] from a formatted string.
68    #[error("failed to parse '{context}' as a time diff: {error}")]
69    FailedToParseTimeDiff {
70        /// Contextual description of where this error occurred including relevant paths,
71        /// filenames, etc.
72        context: &'static str,
73        /// The actual error raised.
74        error: DurationError,
75    },
76
77    /// Failed to parse a [`Timestamp`] from a formatted string.
78    #[error("failed to parse '{context}' as a timestamp: {error}")]
79    FailedToParseTimestamp {
80        /// Contextual description of where this error occurred including relevant paths,
81        /// filenames, etc.
82        context: &'static str,
83        /// The actual error raised.
84        error: TimestampError,
85    },
86
87    /// Failed to parse a `U128`, `U256` or `U512` from a string.
88    #[error("failed to parse '{context}' as u128, u256, or u512: {error:?}")]
89    FailedToParseUint {
90        /// Contextual description of where this error occurred including relevant paths,
91        /// filenames, etc.
92        context: &'static str,
93        /// The actual error raised.
94        error: UIntParseError,
95    },
96
97    /// Failed to parse a `Digest` from a string.
98    #[error("failed to parse '{context}' as a hash digest: {error:?}")]
99    FailedToParseDigest {
100        /// Contextual description of where this error occurred including relevant paths,
101        /// filenames, etc.
102        context: &'static str,
103        /// The actual error raised.
104        error: casper_hashing::Error,
105    },
106
107    /// Failed to create a [`GlobalStateIdentifier`].
108    #[error("failed to parse state identifier")]
109    FailedToParseStateIdentifier,
110
111    /// Conflicting arguments.
112    #[error("conflicting arguments passed '{context}' {args:?}")]
113    ConflictingArguments {
114        /// Contextual description of where this error occurred including relevant paths,
115        /// filenames, etc.
116        context: &'static str,
117        /// Arguments passed, with their values.
118        args: Vec<String>,
119    },
120
121    /// Invalid `CLValue`.
122    #[error("invalid CLValue error: {0}")]
123    InvalidCLValue(String),
124
125    /// Invalid argument.
126    #[error("invalid argument '{context}': {error}")]
127    InvalidArgument {
128        /// Contextual description of where this error occurred including relevant paths,
129        /// filenames, etc.
130        context: &'static str,
131        /// An error message.
132        error: String,
133    },
134
135    /// Error while parsing the json-args from a string to JSON.
136    #[error(
137        "failed to parse json-args to JSON: {0}.  They should be a JSON Array of Objects, each of \
138        the form {{\"name\":<String>,\"type\":<VALUE>,\"value\":<VALUE>}}"
139    )]
140    FailedToParseJsonArgs(#[from] serde_json::Error),
141
142    /// Error while building a [`NamedArg`] from parsed JSON input.
143    #[error(transparent)]
144    JsonArgs(#[from] JsonArgsError),
145
146    /// Core error.
147    #[error(transparent)]
148    Core(#[from] crate::Error),
149}
150
151impl From<CLValueError> for CliError {
152    fn from(error: CLValueError) -> Self {
153        match error {
154            CLValueError::Serialization(bytesrepr_error) => CliError::Core(bytesrepr_error.into()),
155            CLValueError::Type(type_mismatch) => {
156                CliError::InvalidCLValue(type_mismatch.to_string())
157            }
158        }
159    }
160}