casper_client/cli/
error.rs

1use std::{num::ParseIntError, str::ParseBoolError};
2
3use base16::DecodeError;
4use humantime::{DurationError, TimestampError};
5use thiserror::Error;
6
7#[cfg(doc)]
8use casper_types::{
9    account::AccountHash, Key, NamedArg, PublicKey, RuntimeArgs, TimeDiff, Timestamp, URef,
10};
11use casper_types::{CLValueError, KeyFromStrError, UIntParseError, URefFromStrError};
12pub use uint::FromDecStrErr;
13
14use crate::cli::JsonArgsError;
15#[cfg(doc)]
16use crate::rpcs::{DictionaryItemIdentifier, GlobalStateIdentifier};
17
18/// Error that can be returned by the `cli` API.
19#[derive(Error, Debug)]
20pub enum CliError {
21    /// Failed to parse a [`Key`] from a formatted string.
22    #[error("failed to parse {context} as a key: {error}")]
23    FailedToParseKey {
24        /// Contextual description of where this error occurred.
25        context: &'static str,
26        /// The actual error raised.
27        error: KeyFromStrError,
28    },
29
30    /// Failed to parse a [`PublicKey`] from a formatted string.
31    #[error("failed to parse {context} as a public key: {error}")]
32    FailedToParsePublicKey {
33        /// Contextual description of where this error occurred.
34        context: String,
35        /// The actual error raised.
36        error: casper_types::crypto::Error,
37    },
38
39    /// Failed to parse an [`AccountHash`] from a formatted string.
40    #[error("failed to parse {context} as an account hash: {error}")]
41    FailedToParseAccountHash {
42        /// Contextual description of where this error occurred.
43        context: &'static str,
44        /// The actual error raised.
45        error: casper_types::addressable_entity::FromStrError,
46    },
47
48    /// Failed to parse an [`casper_types::AddressableEntityHash`] from a formatted string.
49    #[error("failed to parse {context} as an addressable entity hash: {error}")]
50    FailedToParseAddressableEntityHash {
51        /// Contextual description of where this error occurred.
52        context: &'static str,
53        /// The actual error raised.
54        error: casper_types::addressable_entity::FromStrError,
55    },
56
57    /// Failed to parse a [`URef`] from a formatted string.
58    #[error("failed to parse '{context}' as a uref: {error}")]
59    FailedToParseURef {
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: URefFromStrError,
65    },
66
67    /// Failed to parse an integer from a string.
68    #[error("failed to parse '{context}' as an integer: {error}")]
69    FailedToParseInt {
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: ParseIntError,
75    },
76
77    /// Failed to parse a bool from a string.
78    #[error("failed to parse '{context}' as a bool: {error}")]
79    FailedToParseBool {
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: ParseBoolError,
85    },
86
87    /// Failed to parse an integer from a string.
88    #[error("failed to parse '{context}' as an integer: {error}")]
89    FailedToParseDec {
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: FromDecStrErr,
95    },
96
97    /// Failed to parse a [`TimeDiff`] from a formatted string.
98    #[error("failed to parse '{context}' as a time diff: {error}")]
99    FailedToParseTimeDiff {
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: DurationError,
105    },
106
107    /// Failed to parse a [`Timestamp`] from a formatted string.
108    #[error("failed to parse '{context}' as a timestamp: {error}")]
109    FailedToParseTimestamp {
110        /// Contextual description of where this error occurred including relevant paths,
111        /// filenames, etc.
112        context: &'static str,
113        /// The actual error raised.
114        error: TimestampError,
115    },
116
117    /// Failed to parse a `U128`, `U256` or `U512` from a string.
118    #[error("failed to parse '{context}' as u128, u256, or u512: {error:?}")]
119    FailedToParseUint {
120        /// Contextual description of where this error occurred including relevant paths,
121        /// filenames, etc.
122        context: &'static str,
123        /// The actual error raised.
124        error: UIntParseError,
125    },
126
127    /// Failed to parse a `Digest` from a string.
128    #[error("failed to parse '{context}' as a hash digest: {error:?}")]
129    FailedToParseDigest {
130        /// Contextual description of where this error occurred including relevant paths,
131        /// filenames, etc.
132        context: &'static str,
133        /// The actual error raised.
134        error: casper_types::DigestError,
135    },
136
137    /// Failed to create a [`GlobalStateIdentifier`].
138    #[error("failed to parse state identifier")]
139    FailedToParseStateIdentifier,
140
141    /// Conflicting arguments.
142    #[error("conflicting arguments passed '{context}' {args:?}")]
143    ConflictingArguments {
144        /// Contextual description of where this error occurred including relevant paths,
145        /// filenames, etc.
146        context: String,
147        /// Arguments passed, with their values.
148        args: Vec<String>,
149    },
150
151    /// Invalid `CLValue`.
152    #[error("invalid CLValue error: {0}")]
153    InvalidCLValue(String),
154
155    /// Invalid argument.
156    #[error("invalid argument '{context}': {error}")]
157    InvalidArgument {
158        /// Contextual description of where this error occurred including relevant paths,
159        /// filenames, etc.
160        context: &'static str,
161        /// An error message.
162        error: String,
163    },
164
165    /// Error while parsing the json-args from a string to JSON.
166    #[error(
167        "failed to parse json-args to JSON: {0}.  They should be a JSON Array of Objects, each of \
168        the form {{\"name\":<String>,\"type\":<VALUE>,\"value\":<VALUE>}}"
169    )]
170    FailedToParseJsonArgs(#[from] serde_json::Error),
171
172    /// Error while building a [`NamedArg`] from parsed JSON input.
173    #[error(transparent)]
174    JsonArgs(#[from] JsonArgsError),
175
176    /// Core error.
177    #[error(transparent)]
178    Core(#[from] crate::Error),
179
180    /// Failed to parse a package address
181    #[error("Failed to parse a package address")]
182    FailedToParsePackageAddr,
183
184    /// Failed to parse a transfer target
185    #[error("Failed to parse a transfer target")]
186    FailedToParseTransferTarget,
187
188    /// Failed to parse a validator public key.
189    #[error("Failed to parse a validator public key")]
190    FailedToParseValidatorPublicKey,
191
192    /// Failed to parse base16 bytes.
193    #[error("Failed to parse base16 bytes: {0}")]
194    FailedToParseBase16(#[from] DecodeError),
195
196    /// Unexpected transaction args variant.
197    #[error("Unexpected transaction args variant")]
198    UnexpectedTransactionArgsVariant,
199
200    /// Failed to get auction info to determine validator stake.
201    #[error("Failed to get auction state")]
202    FailedToGetAuctionState,
203
204    /// Attempting to withdraw bid will reduce stake below the minimum amount.
205    #[error("Attempting to withdraw bid will reduce stake below the minimum amount.")]
206    ReducedStakeBelowMinAmount,
207
208    /// Failed to parse the chainspec as raw bytes.
209    #[error("Failed to parse the chainspec as raw bytes")]
210    FailedToParseChainspecBytes,
211
212    /// Missing major version.
213    #[error("Major version is missing when specifying entity version")]
214    MissingMajorVersion,
215}
216
217impl From<CLValueError> for CliError {
218    fn from(error: CLValueError) -> Self {
219        match error {
220            CLValueError::Serialization(bytesrepr_error) => CliError::Core(bytesrepr_error.into()),
221            CLValueError::Type(type_mismatch) => {
222                CliError::InvalidCLValue(type_mismatch.to_string())
223            }
224        }
225    }
226}