tenderly_rs/
errors.rs

1use thiserror::Error;
2
3/// Tenderly error type
4#[derive(Debug, Error)]
5#[error("Tenderly Error: {message} (slug: {slug})")]
6pub struct TenderlyError {
7    pub id: Option<String>,
8    pub message: String,
9    pub slug: String,
10}
11
12/// General error type
13#[derive(Debug, Error)]
14pub enum GeneralError {
15    #[error("API Error: {0}")]
16    ApiError(ApiError),
17
18    #[error("Invalid Arguments: {0}")]
19    InvalidArguments(InvalidArgumentsError),
20
21    #[error("Not Found: {0}")]
22    NotFound(NotFoundError),
23
24    #[error("Invalid Response: {0}")]
25    InvalidResponse(InvalidResponseError),
26
27    #[error("Compilation Error: {0}")]
28    Compilation(CompilationError),
29
30    #[error("Bytecode Mismatch: {0}")]
31    BytecodeMismatch(BytecodeMismatchError),
32
33    #[error("Encoding Error: {0}")]
34    Encoding(EncodingError),
35
36    #[error("Unexpected Verification Error: {0}")]
37    UnexpectedVerification(UnexpectedVerificationError),
38
39    #[error("Unknown error: {0}")]
40    Unknown(String),
41}
42
43/// API Error
44#[derive(Debug, Error)]
45#[error("API Error (status: {status}): {error}")]
46pub struct ApiError {
47    pub status: u16,
48    pub error: TenderlyError,
49}
50
51/// Invalid Arguments Error
52#[derive(Debug, Error)]
53#[error("Invalid Arguments: {message}")]
54pub struct InvalidArgumentsError {
55    pub message: String,
56}
57
58/// Not Found Error
59#[derive(Debug, Error)]
60#[error("Not Found: {message}")]
61pub struct NotFoundError {
62    pub message: String,
63}
64
65/// Invalid Response Error
66#[derive(Debug, Error)]
67#[error("Invalid Response: {message}")]
68pub struct InvalidResponseError {
69    pub message: String,
70}
71
72/// Compilation Error
73#[derive(Debug, Error)]
74#[error("Compilation Error: {message}")]
75pub struct CompilationError {
76    pub message: String,
77    pub data: Option<serde_json::Value>,
78}
79
80/// Bytecode Mismatch Error
81#[derive(Debug, Error)]
82#[error("Bytecode Mismatch: {message}")]
83pub struct BytecodeMismatchError {
84    pub message: String,
85    pub data: Option<serde_json::Value>,
86}
87
88/// Encoding Error
89#[derive(Debug, Error)]
90#[error("Encoding Error: {error}")]
91pub struct EncodingError {
92    pub error: TenderlyError,
93}
94
95/// Unexpected Verification Error
96#[derive(Debug, Error)]
97#[error("Unexpected Verification Error: {message}")]
98pub struct UnexpectedVerificationError {
99    pub message: String,
100}
101
102impl InvalidArgumentsError {
103    pub fn new(message: impl Into<String>) -> Self {
104        Self {
105            message: message.into(),
106        }
107    }
108}
109
110impl NotFoundError {
111    pub fn new(message: impl Into<String>) -> Self {
112        Self {
113            message: message.into(),
114        }
115    }
116}
117
118impl InvalidResponseError {
119    pub fn new(message: impl Into<String>) -> Self {
120        Self {
121            message: message.into(),
122        }
123    }
124}
125
126impl CompilationError {
127    pub fn new(message: impl Into<String>, data: Option<serde_json::Value>) -> Self {
128        Self {
129            message: message.into(),
130            data,
131        }
132    }
133}
134
135impl BytecodeMismatchError {
136    pub fn new(message: impl Into<String>, data: Option<serde_json::Value>) -> Self {
137        Self {
138            message: message.into(),
139            data,
140        }
141    }
142}
143
144impl UnexpectedVerificationError {
145    pub fn new(message: impl Into<String>) -> Self {
146        Self {
147            message: message.into(),
148        }
149    }
150}
151
152impl EncodingError {
153    pub fn new(error: TenderlyError) -> Self {
154        Self { error }
155    }
156}
157
158impl ApiError {
159    pub fn new(status: u16, error: TenderlyError) -> Self {
160        Self { status, error }
161    }
162}
163
164impl From<reqwest::Error> for GeneralError {
165    fn from(error: reqwest::Error) -> Self {
166        GeneralError::Unknown(format!("HTTP Error: {}", error))
167    }
168}
169
170impl From<serde_json::Error> for GeneralError {
171    fn from(error: serde_json::Error) -> Self {
172        GeneralError::Unknown(format!("JSON Error: {}", error))
173    }
174}