casper_client/cli/json_args/
error.rs1use std::num::ParseIntError;
2
3use serde_json::Value;
4use thiserror::Error;
5
6#[cfg(doc)]
7use casper_types::{account::AccountHash, CLValue, Key, NamedArg, PublicKey, URef};
8use casper_types::{
9 bytesrepr::Error as BytesreprError, crypto::Error as CryptoError, CLType, KeyFromStrError,
10 URefFromStrError,
11};
12
13#[derive(Error, Debug)]
15#[error(
16 "failed to construct a CLValue of type {cl_type:?} from {json_value} for arg {arg_name}: \
17 {details}"
18)]
19pub struct Error {
20 pub arg_name: String,
22 pub cl_type: CLType,
24 pub json_value: Value,
26 pub details: ErrorDetails,
28}
29
30impl Error {
31 pub(super) fn new(
32 arg_name: String,
33 cl_type: CLType,
34 json_value: Value,
35 details: ErrorDetails,
36 ) -> Self {
37 Error {
38 arg_name,
39 cl_type,
40 json_value,
41 details,
42 }
43 }
44}
45
46#[derive(Error, Debug)]
48pub enum ErrorDetails {
49 #[error("failed bytesrepr encoding: {0}")]
51 Bytesrepr(BytesreprError),
52
53 #[error("cannot convert given JSON Number to `i32`")]
55 CannotParseToI32,
56
57 #[error("cannot convert given JSON Number to `i64`")]
59 CannotParseToI64,
60
61 #[error("cannot convert given JSON Number to `u8`")]
63 CannotParseToU8,
64
65 #[error("cannot convert given JSON Number to `u32`")]
67 CannotParseToU32,
68
69 #[error("cannot convert given JSON Number to `u64`")]
71 CannotParseToU64,
72
73 #[error(transparent)]
75 ParseInt(#[from] ParseIntError),
76
77 #[error(transparent)]
79 ParseBigint(#[from] uint::FromDecStrErr),
80
81 #[error("failed parsing a Key: {0}")]
83 ParseKeyFromString(KeyFromStrError),
84
85 #[error(
89 "invalid number of fields: JSON Object representing a Key must have exactly one field, the \
90 name of the Key variant mapped to the Key as a formatted string"
91 )]
92 KeyObjectHasInvalidNumberOfFields,
93
94 #[error(
98 "invalid field type: JSON Object representing a Key must have exactly one field, the name \
99 of the Key variant mapped to the Key as a formatted string"
100 )]
101 KeyObjectHasInvalidFieldType,
102
103 #[error(
107 "invalid Key variant: JSON Object representing a Key must have exactly one field, the name \
108 of the Key variant mapped to the Key as a formatted string"
109 )]
110 KeyObjectHasInvalidVariant,
111
112 #[error("failed parsing a URef: {0}")]
114 ParseURef(URefFromStrError),
115
116 #[error(transparent)]
118 ParsePublicKey(#[from] CryptoError),
119
120 #[error(transparent)]
122 HexDecode(#[from] base16::DecodeError),
123
124 #[error("number of hex-decoded bytes ({actual_length}) not as expected ({expected_length})")]
126 ByteArrayLengthMismatch {
127 expected_length: u32,
129 actual_length: u32,
131 },
132
133 #[error(
136 "invalid number of fields: JSON Object representing a Result must have exactly one field \
137 named 'Ok' or 'Err'"
138 )]
139 ResultObjectHasInvalidNumberOfFields,
140
141 #[error(
144 "invalid Result variant: JSON Object representing a Result must have exactly one field \
145 named 'Ok' or 'Err'"
146 )]
147 ResultObjectHasInvalidVariant,
148
149 #[error(
152 "invalid map key type ({0:?}): only maps with key types of string or number can be \
153 represented as JSON Objects, maps with more complex key types must use a JSON Array"
154 )]
155 MapTypeNotValidAsObject(CLType),
156
157 #[error("invalid entry type: JSON Array representing a Map must have all entries as Objects")]
160 MapArrayHasInvalidEntryType,
161
162 #[error(
165 "invalid number of fields: JSON Object representing a Map entry must have exactly two \
166 fields, named 'key' and 'value'"
167 )]
168 MapEntryObjectHasInvalidNumberOfFields,
169
170 #[error(
173 "missing key field: JSON Object representing a Map entry must have exactly two fields, \
174 named 'key' and 'value'"
175 )]
176 MapEntryObjectMissingKeyField,
177
178 #[error(
181 "missing value field: JSON Object representing a Map entry must have exactly two fields, \
182 named 'key' and 'value'"
183 )]
184 MapEntryObjectMissingValueField,
185
186 #[error("number of tuple entries ({actual}) not as expected ({expected})")]
188 TupleEntryCountMismatch {
189 expected: usize,
191 actual: usize,
193 },
194
195 #[error("the given CLType cannot be constructed from the given type of JSON value")]
197 IncompatibleType,
198}
199
200impl From<KeyFromStrError> for ErrorDetails {
201 fn from(error: KeyFromStrError) -> Self {
202 ErrorDetails::ParseKeyFromString(error)
203 }
204}
205
206impl From<URefFromStrError> for ErrorDetails {
207 fn from(error: URefFromStrError) -> Self {
208 ErrorDetails::ParseURef(error)
209 }
210}
211
212impl From<BytesreprError> for ErrorDetails {
213 fn from(error: BytesreprError) -> Self {
214 ErrorDetails::Bytesrepr(error)
215 }
216}