Skip to main content

datex_core/runtime/execution/
errors.rs

1use crate::{
2    dxb_parser::body::DXBParserError,
3    global::protocol_structures::instruction_data::StackIndex,
4    libs::core::core_lib_id::CoreLibIdIndex,
5    network::com_hub::network_response::ResponseError,
6    prelude::*,
7    runtime::{
8        cache::shared_values_cache::CacheValueRetrievalError,
9        execution::execution_loop::state::ExecutionLoopState,
10    },
11    shared_values::errors::{
12        AccessError, AssignmentError, SharedValueCreationError,
13    },
14    traits::apply::ApplyError,
15    types::error::IllegalTypeError,
16    value_updates::errors::UpdateError,
17    values::value_container::{ValueContainer, error::ValueError},
18};
19use core::fmt::Display;
20
21#[derive(Debug, Clone, PartialEq, Eq)]
22pub enum InvalidProgramError {
23    // any unterminated sequence, e.g. missing key in key-value pair
24    UnterminatedSequence,
25    MissingRemoteExecutionReceiver,
26    ExpectedTypeValue,
27    ExpectedAliasType,
28    ExpectedValue,
29    ExpectedList,
30    ExpectedInstruction,
31    ExpectedRegularInstruction,
32    ExpectedTypeInstruction,
33    InvalidCoreLibId(CoreLibIdIndex),
34}
35
36impl Display for InvalidProgramError {
37    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
38        match self {
39            InvalidProgramError::ExpectedAliasType => {
40                core::write!(f, "Expected an alias type definition")
41            }
42            InvalidProgramError::UnterminatedSequence => {
43                core::write!(f, "Unterminated sequence")
44            }
45            InvalidProgramError::MissingRemoteExecutionReceiver => {
46                core::write!(f, "Missing remote execution receiver")
47            }
48            InvalidProgramError::ExpectedTypeValue => {
49                core::write!(f, "Expected a type value")
50            }
51            InvalidProgramError::ExpectedValue => {
52                core::write!(f, "Expected a value")
53            }
54            InvalidProgramError::ExpectedRegularInstruction => {
55                core::write!(f, "Expected a regular instruction")
56            }
57            InvalidProgramError::ExpectedTypeInstruction => {
58                core::write!(f, "Expected a type instruction")
59            }
60            InvalidProgramError::ExpectedInstruction => {
61                core::write!(f, "Expected an instruction")
62            }
63            InvalidProgramError::ExpectedList => {
64                core::write!(f, "Expected a list")
65            }
66            InvalidProgramError::InvalidCoreLibId(core_lib_id) => {
67                core::write!(f, "Invalid core library id: {core_lib_id}")
68            }
69        }
70    }
71}
72
73#[derive(Debug)]
74pub enum ExecutionError {
75    Unspecified(String), // TODO: replace with nested stack trace exceptions
76    DXBParserError(DXBParserError),
77    ValueError(ValueError),
78    InvalidProgram(InvalidProgramError),
79    AccessError(AccessError),
80    CacheValueRetrievalError(CacheValueRetrievalError),
81    UpdateError(UpdateError),
82    Unknown,
83    NotImplemented(String),
84    StackValueNotAllocated(StackIndex),
85    StackOutOfBoundsAccess(StackIndex),
86    InternalSlotDoesNotExist(u32),
87    RequiresAsyncExecution,
88    ResponseError(ResponseError),
89    IllegalTypeError(IllegalTypeError),
90    ReferenceNotFound,
91    InvalidUnbox,
92    InvalidTypeCast,
93    ExpectedTypeValue,
94    InvalidSharedValueType,
95    ExpectedSharedValue,
96    ExpectedLocalValue,
97    ExpectedOwnedSharedValue,
98    MutableReferenceToNonMutableValue,
99    AssignmentError(AssignmentError),
100    ReferenceCreationError(SharedValueCreationError),
101    IntermediateResultWithState(
102        Option<ValueContainer>,
103        Option<ExecutionLoopState>,
104    ),
105    ApplyError(ApplyError),
106    UnauthorizedMove,
107    InvalidMove,
108    MoveToMultipleEndpoints,
109}
110
111impl From<ApplyError> for ExecutionError {
112    fn from(error: ApplyError) -> Self {
113        ExecutionError::ApplyError(error)
114    }
115}
116
117impl From<SharedValueCreationError> for ExecutionError {
118    fn from(error: SharedValueCreationError) -> Self {
119        ExecutionError::ReferenceCreationError(error)
120    }
121}
122
123impl From<CacheValueRetrievalError> for ExecutionError {
124    fn from(err: CacheValueRetrievalError) -> Self {
125        ExecutionError::CacheValueRetrievalError(err)
126    }
127}
128
129impl From<AccessError> for ExecutionError {
130    fn from(error: AccessError) -> Self {
131        ExecutionError::AccessError(error)
132    }
133}
134
135impl From<UpdateError> for ExecutionError {
136    fn from(error: UpdateError) -> Self {
137        ExecutionError::UpdateError(error)
138    }
139}
140
141impl From<DXBParserError> for ExecutionError {
142    fn from(error: DXBParserError) -> Self {
143        ExecutionError::DXBParserError(error)
144    }
145}
146
147impl From<ValueError> for ExecutionError {
148    fn from(error: ValueError) -> Self {
149        ExecutionError::ValueError(error)
150    }
151}
152
153impl From<IllegalTypeError> for ExecutionError {
154    fn from(error: IllegalTypeError) -> Self {
155        ExecutionError::IllegalTypeError(error)
156    }
157}
158
159impl From<InvalidProgramError> for ExecutionError {
160    fn from(error: InvalidProgramError) -> Self {
161        ExecutionError::InvalidProgram(error)
162    }
163}
164
165impl From<ResponseError> for ExecutionError {
166    fn from(error: ResponseError) -> Self {
167        ExecutionError::ResponseError(error)
168    }
169}
170
171impl From<AssignmentError> for ExecutionError {
172    fn from(error: AssignmentError) -> Self {
173        ExecutionError::AssignmentError(error)
174    }
175}
176
177impl Display for ExecutionError {
178    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
179        match self {
180            ExecutionError::ReferenceCreationError(err) => {
181                core::write!(f, "Reference from value container error: {err}")
182            }
183            ExecutionError::ApplyError(err) => {
184                core::write!(f, "Apply error: {err}")
185            }
186            ExecutionError::ReferenceNotFound => {
187                core::write!(f, "Reference not found")
188            }
189            ExecutionError::DXBParserError(err) => {
190                core::write!(f, "Parser error: {err}")
191            }
192            ExecutionError::Unknown => {
193                core::write!(f, "Unknown execution error")
194            }
195            ExecutionError::ValueError(err) => {
196                core::write!(f, "Value error: {err}")
197            }
198            ExecutionError::InvalidProgram(err) => {
199                core::write!(f, "Invalid program error: {err}")
200            }
201            ExecutionError::NotImplemented(msg) => {
202                core::write!(f, "Not implemented: {msg}")
203            }
204            ExecutionError::RequiresAsyncExecution => {
205                core::write!(f, "Program must be executed asynchronously")
206            }
207            ExecutionError::ResponseError(err) => {
208                core::write!(f, "Response error: {err}")
209            }
210            ExecutionError::IllegalTypeError(err) => {
211                core::write!(f, "Illegal type: {err}")
212            }
213            ExecutionError::InvalidUnbox => {
214                core::write!(f, "Tried to unbox a non-reference value")
215            }
216            ExecutionError::AssignmentError(err) => {
217                core::write!(f, "Assignment error: {err}")
218            }
219            ExecutionError::InvalidTypeCast => {
220                core::write!(f, "Invalid type cast")
221            }
222            ExecutionError::ExpectedTypeValue => {
223                core::write!(f, "Expected a type value")
224            }
225            ExecutionError::InvalidSharedValueType => {
226                core::write!(f, "Invalid shared value type")
227            }
228            ExecutionError::AccessError(err) => {
229                core::write!(f, "Access error: {err}")
230            }
231            ExecutionError::CacheValueRetrievalError(err) => {
232                core::write!(f, "Cache value retrieval error: {err}")
233            }
234            ExecutionError::IntermediateResultWithState(
235                value_opt,
236                state_opt,
237            ) => {
238                core::write!(
239                    f,
240                    "Execution produced an intermediate result: {:?} with state: {:?}",
241                    value_opt,
242                    state_opt
243                )
244            }
245            ExecutionError::UnauthorizedMove => {
246                core::write!(f, "Unauthorized move of shared pointer")
247            }
248            ExecutionError::InvalidMove => {
249                core::write!(f, "Invalid move of shared pointer")
250            }
251            ExecutionError::MoveToMultipleEndpoints => {
252                core::write!(f, "Illegal move to multiple endpoints")
253            }
254            ExecutionError::ExpectedSharedValue => {
255                core::write!(
256                    f,
257                    "Expected a shared value, but got a local value"
258                )
259            }
260            ExecutionError::ExpectedLocalValue => {
261                core::write!(
262                    f,
263                    "Expected a local value, but got a shared value"
264                )
265            }
266            ExecutionError::ExpectedOwnedSharedValue => {
267                core::write!(
268                    f,
269                    "Expected an owned shared value, but got a non-owned shared value"
270                )
271            }
272            ExecutionError::MutableReferenceToNonMutableValue => {
273                core::write!(
274                    f,
275                    "Tried to create a mutable reference to a non-mutable value"
276                )
277            }
278            ExecutionError::StackValueNotAllocated(index) => {
279                core::write!(
280                    f,
281                    "Tried to access unallocated stack value at index {index}"
282                )
283            }
284            ExecutionError::StackOutOfBoundsAccess(index) => {
285                core::write!(
286                    f,
287                    "Tried to access out of bounds stack value at index {index}"
288                )
289            }
290            ExecutionError::InternalSlotDoesNotExist(index) => {
291                core::write!(f, "Internal slot does not exist at index {index}")
292            }
293            ExecutionError::UpdateError(err) => {
294                core::write!(f, "Value update error: {err}")
295            }
296            ExecutionError::Unspecified(msg) => {
297                core::write!(f, "Unspecified error: {msg}")
298            }
299        }
300    }
301}