snarkvm_synthesizer_error/
process.rs1use crate::{EvalError, ExecError, FinalizeError};
17use anyhow::Error;
18use snarkvm_circuit_environment::ConstraintUnsatisfied;
19use snarkvm_console_network::Network;
20use snarkvm_console_program::{Identifier, ProgramID};
21use thiserror::Error;
22
23#[derive(Debug, Error)]
28pub enum ProcessAuthError {
29 #[error("Stack authorization failed: {0}")]
31 StackAuth(#[from] StackAuthError),
32 #[error(transparent)]
34 Anyhow(#[from] anyhow::Error),
35}
36
37#[derive(Debug, Error)]
39pub enum ProcessEvalError {
40 #[error("Stack evaluation failed: {0}")]
42 StackEval(#[from] StackEvalError),
43 #[error(transparent)]
45 Anyhow(#[from] anyhow::Error),
46}
47
48#[derive(Debug, Error)]
50pub enum ProcessExecError {
51 #[error("Stack execution failed: {0}")]
53 StackExec(#[from] StackExecError),
54 #[error(transparent)]
56 Anyhow(#[from] anyhow::Error),
57}
58
59#[derive(Debug, Error)]
61pub enum ProcessDeployError {
62 #[error("Stack synthesis failed: {0}")]
64 StackExec(#[from] StackExecError),
65 #[error(transparent)]
67 Anyhow(#[from] anyhow::Error),
68}
69
70#[derive(Debug, Error)]
72pub enum CallEvalError {
73 #[error("Substack evaluation failed: {0}")]
75 StackEval(#[from] StackEvalError),
76 #[error(transparent)]
78 Anyhow(#[from] anyhow::Error),
79}
80
81#[derive(Debug, Error)]
83pub enum CallExecError {
84 #[error("Substack execution failed: {0}")]
86 StackExec(#[from] StackExecError),
87 #[error("Substack evaluation failed: {0}")]
89 StackEval(#[from] StackEvalError),
90 #[error(transparent)]
92 Constraint(#[from] ConstraintUnsatisfied),
93 #[error(transparent)]
95 Anyhow(#[from] anyhow::Error),
96}
97
98#[derive(Debug, Error)]
100pub enum StackAuthError {
101 #[error("Stack execution failed: {0}")]
103 Exec(#[from] StackExecError),
104 #[error("Stack evaluation failed: {0}")]
106 Eval(#[from] StackEvalError),
107 #[error(transparent)]
109 Anyhow(#[from] anyhow::Error),
110}
111
112#[derive(Debug, Error)]
114pub enum StackExecError {
115 #[error(transparent)]
117 Instruction(#[from] IndexedInstructionError<InstructionError>),
118 #[error(transparent)]
120 Constraint(#[from] ConstraintUnsatisfied),
121 #[error(transparent)]
123 Anyhow(#[from] anyhow::Error),
124}
125
126#[derive(Debug, Error)]
128pub enum StackEvalError {
129 #[error(transparent)]
131 Instruction(#[from] IndexedInstructionError<InstructionEvalError>),
132 #[error(transparent)]
134 Anyhow(#[from] anyhow::Error),
135}
136
137#[derive(Debug, Error)]
139#[error("Instruction ({instruction}) at index {index} failed: {error}")]
140pub struct IndexedInstructionError<E> {
141 pub index: usize,
143 pub instruction: String,
145 pub error: E,
147}
148
149#[derive(Debug, Error)]
152pub enum InstructionError {
153 #[error("Failed to evaluate: {0}")]
155 Eval(#[from] InstructionEvalError),
156 #[error("Failed to execute: {0}")]
158 Exec(#[from] InstructionExecError),
159}
160
161#[derive(Debug, Error)]
163pub enum InstructionEvalError {
164 #[error(transparent)]
166 Eval(#[from] EvalError),
167 #[error("Call failed: {0}")]
169 Call(#[from] Box<CallEvalError>),
170 #[error(transparent)]
172 Anyhow(#[from] anyhow::Error),
173}
174
175#[derive(Debug, Error)]
177pub enum InstructionExecError {
178 #[error("Call failed: {0}")]
180 Call(#[from] Box<CallExecError>),
181 #[error(transparent)]
183 Exec(#[from] ExecError),
184 #[error(transparent)]
186 Anyhow(#[from] anyhow::Error),
187}
188
189impl<E> IndexedInstructionError<E> {
190 pub fn new(index: usize, instruction: String, error: E) -> Self {
192 Self { index, instruction, error }
193 }
194}
195
196pub struct IndexedFinalizeError<N: Network, C: ToString> {
201 pub program_id: Option<(ProgramID<N>, u16)>,
203 pub resource: Option<Identifier<N>>,
205 pub command: Option<Box<(usize, C)>>,
207 pub error: FinalizeError,
209}
210
211impl<N: Network, C: ToString> std::fmt::Debug for IndexedFinalizeError<N, C> {
212 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
213 std::fmt::Display::fmt(self, f)
214 }
215}
216
217impl<N: Network, C: ToString> std::fmt::Display for IndexedFinalizeError<N, C> {
218 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
219 let locator = match (&self.program_id, &self.resource) {
221 (Some((program_id, _)), Some(resource)) => format!("{program_id}/{resource}"),
222 (Some((program_id, _)), None) => format!("{program_id}"),
223 (None, Some(resource)) => format!("{resource}"),
224 (None, None) => "None".to_string(),
225 };
226 match &self.command {
227 Some(cmd) => {
228 let (index, command) = cmd.as_ref();
229 write!(
230 f,
231 "Failed to finalize '{locator}' command ({}) at index {index}: {}",
232 command.to_string(),
233 self.error
234 )
235 }
236 None => write!(f, "Failed to finalize '{locator}': {}", self.error),
237 }
238 }
239}
240
241impl<N: Network, C: ToString> std::error::Error for IndexedFinalizeError<N, C> {}
242
243impl<N: Network, C: ToString> From<Error> for IndexedFinalizeError<N, C> {
244 fn from(error: Error) -> Self {
246 Self::new(None, None, None, FinalizeError::Anyhow(error))
247 }
248}
249
250impl<N: Network, C: ToString> IndexedFinalizeError<N, C> {
251 pub fn new(
253 program_id: Option<(ProgramID<N>, u16)>,
254 resource: Option<Identifier<N>>,
255 command: Option<(usize, C)>,
256 error: FinalizeError,
257 ) -> Self {
258 Self { program_id, resource, command: command.map(Box::new), error }
259 }
260}
261
262#[macro_export]
270macro_rules! indexed_finalize_bail {
271 ($program_id:expr, $resource:expr, $index:expr, $command:expr, $($arg:tt)+) => {{
273 return Err(IndexedFinalizeError::new(
274 $program_id,
275 $resource,
276 Some(($index, $command)),
277 FinalizeError::Anyhow(anyhow!($($arg)+)),
278 ));
279 }};
280 ($program_id:expr, $resource:expr, $($arg:tt)+) => {{
282 return Err(IndexedFinalizeError::new(
283 $program_id,
284 $resource,
285 None,
286 FinalizeError::Anyhow(anyhow!($($arg)+)),
287 ));
288 }};
289}
290
291pub trait IntoIndexedFinalize<N: Network, C: ToString, T> {
292 fn into_indexed(
293 self,
294 program_id: Option<(ProgramID<N>, u16)>,
295 resource: Option<Identifier<N>>,
296 command: Option<(usize, C)>,
297 ) -> anyhow::Result<T, IndexedFinalizeError<N, C>>;
298}
299
300impl<N: Network, C: ToString, T> IntoIndexedFinalize<N, C, T> for anyhow::Result<T, Error> {
301 fn into_indexed(
302 self,
303 program_id: Option<(ProgramID<N>, u16)>,
304 resource: Option<Identifier<N>>,
305 command: Option<(usize, C)>,
306 ) -> anyhow::Result<T, IndexedFinalizeError<N, C>> {
307 self.map_err(|e| IndexedFinalizeError::new(program_id, resource, command, FinalizeError::Anyhow(e)))
308 }
309}