1use crate::ast::Position;
2use serde::{Deserialize, Serialize};
3
4#[derive(Debug, Clone, Serialize, Deserialize)]
5pub enum ApiError {
6 CompilationError {
7 message: String,
8 position: Option<Position>,
9 },
10
11 ExecutionError {
12 message: String,
13 position: Option<Position>,
14 },
15
16 InvalidInput {
17 message: String,
18 input: String,
19 position: Option<Position>,
20 },
21
22 EngineError {
23 message: String,
24 position: Option<Position>,
25 },
26
27 InterpreterError {
28 message: String,
29 position: Option<Position>,
30 },
31
32 ConfigurationError {
33 message: String,
34 position: Option<Position>,
35 },
36
37 ResourceError {
38 resource: String,
39 message: String,
40 position: Option<Position>,
41 },
42
43 TimeoutError {
44 operation: String,
45 timeout_ms: u64,
46 position: Option<Position>,
47 },
48}
49
50impl std::fmt::Display for ApiError {
51 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
52 match self {
53 ApiError::CompilationError { message, position } => {
54 write!(f, "Compilation error: {message}")?;
55 if let Some(pos) = position {
56 write!(f, " at line {}, column {}", pos.line, pos.column)?;
57 }
58 Ok(())
59 }
60 ApiError::ExecutionError { message, position } => {
61 write!(f, "Execution error: {message}")?;
62 if let Some(pos) = position {
63 write!(f, " at line {}, column {}", pos.line, pos.column)?;
64 }
65 Ok(())
66 }
67 ApiError::InvalidInput {
68 message,
69 input,
70 position,
71 } => {
72 write!(f, "Invalid input '{input}': {message}")?;
73 if let Some(pos) = position {
74 write!(f, " at line {}, column {}", pos.line, pos.column)?;
75 }
76 Ok(())
77 }
78 ApiError::EngineError { message, position } => {
79 write!(f, "Engine error: {message}")?;
80 if let Some(pos) = position {
81 write!(f, " at line {}, column {}", pos.line, pos.column)?;
82 }
83 Ok(())
84 }
85 ApiError::InterpreterError { message, position } => {
86 write!(f, "Interpreter error: {message}")?;
87 if let Some(pos) = position {
88 write!(f, " at line {}, column {}", pos.line, pos.column)?;
89 }
90 Ok(())
91 }
92 ApiError::ConfigurationError { message, position } => {
93 write!(f, "Configuration error: {message}")?;
94 if let Some(pos) = position {
95 write!(f, " at line {}, column {}", pos.line, pos.column)?;
96 }
97 Ok(())
98 }
99 ApiError::ResourceError {
100 resource,
101 message,
102 position,
103 } => {
104 write!(f, "Resource '{resource}' error: {message}")?;
105 if let Some(pos) = position {
106 write!(f, " at line {}, column {}", pos.line, pos.column)?;
107 }
108 Ok(())
109 }
110 ApiError::TimeoutError {
111 operation,
112 timeout_ms,
113 position,
114 } => {
115 write!(f, "Operation '{operation}' timed out after {timeout_ms}ms",)?;
116 if let Some(pos) = position {
117 write!(f, " at line {}, column {}", pos.line, pos.column)?;
118 }
119 Ok(())
120 }
121 }
122 }
123}
124
125impl std::error::Error for ApiError {}