Skip to main content

datex_core/runtime/execution/
errors.rs

1use crate::{
2    dxb_parser::body::DXBParserError,
3    global::stack_index::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        remote_value_sync::SubscriberError,
11    },
12    shared_values::errors::{
13        AccessError, AssignmentError, SharedValueCreationError,
14    },
15    traits::apply::ApplyError,
16    types::error::IllegalTypeError,
17    value_updates::errors::UpdateError,
18    values::value_container::{ValueContainer, error::ValueError},
19};
20use core::fmt::Display;
21
22#[derive(Debug, Clone, PartialEq, Eq)]
23pub enum InvalidProgramError {
24    // any unterminated sequence, e.g. missing key in key-value pair
25    UnterminatedSequence,
26    MissingRemoteExecutionReceiver,
27    ExpectedTypeValue,
28    ExpectedAliasType,
29    ExpectedValue,
30    ExpectedList,
31    ExpectedInstruction,
32    ExpectedRegularInstruction,
33    ExpectedTypeInstruction,
34    InvalidCoreLibId(CoreLibIdIndex),
35    InvalidInstructionFormat,
36    InvalidType,
37}
38
39impl Display for InvalidProgramError {
40    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
41        match self {
42            InvalidProgramError::ExpectedAliasType => {
43                core::write!(f, "Expected an alias type definition")
44            }
45            InvalidProgramError::UnterminatedSequence => {
46                core::write!(f, "Unterminated sequence")
47            }
48            InvalidProgramError::MissingRemoteExecutionReceiver => {
49                core::write!(f, "Missing remote execution receiver")
50            }
51            InvalidProgramError::ExpectedTypeValue => {
52                core::write!(f, "Expected a type value")
53            }
54            InvalidProgramError::ExpectedValue => {
55                core::write!(f, "Expected a value")
56            }
57            InvalidProgramError::ExpectedRegularInstruction => {
58                core::write!(f, "Expected a regular instruction")
59            }
60            InvalidProgramError::ExpectedTypeInstruction => {
61                core::write!(f, "Expected a type instruction")
62            }
63            InvalidProgramError::ExpectedInstruction => {
64                core::write!(f, "Expected an instruction")
65            }
66            InvalidProgramError::ExpectedList => {
67                core::write!(f, "Expected a list")
68            }
69            InvalidProgramError::InvalidCoreLibId(core_lib_id) => {
70                core::write!(f, "Invalid core library id: {core_lib_id}")
71            }
72            InvalidProgramError::InvalidInstructionFormat => {
73                core::write!(f, "Invalid instruction format")
74            }
75            InvalidProgramError::InvalidType => {
76                core::write!(f, "Invalid type")
77            }
78        }
79    }
80}
81
82#[derive(Debug, PartialEq, Eq)]
83pub enum ExecutionError {
84    Unspecified(String), // TODO: replace with nested stack trace exceptions
85    DXBParserError(Box<DXBParserError>),
86    ValueError(Box<ValueError>),
87    InvalidProgram(Box<InvalidProgramError>),
88    AccessError(Box<AccessError>),
89    MethodNotFound(String),
90    CacheValueRetrievalError(Box<CacheValueRetrievalError>),
91    UpdateError(Box<UpdateError>),
92    SubscriberError(Box<SubscriberError>),
93    Unknown,
94    InvalidExecutionState,
95    NotImplemented(String),
96    StackValueNotAllocated(StackIndex),
97    StackOutOfBoundsAccess(StackIndex),
98    InternalSlotDoesNotExist(u32),
99    RequiresAsyncExecution,
100    ResponseError(Box<ResponseError>),
101    IllegalTypeError(Box<IllegalTypeError>),
102    ReferenceNotFound,
103    InvalidUnbox,
104    InvalidTypeCast,
105    ExpectedTypeValue,
106    InvalidSharedValueType,
107    ExpectedSharedValue,
108    ExpectedLocalValue,
109    ExpectedEntityValue,
110    ExpectedOwnedSharedValue,
111    MutableReferenceToNonMutableValue,
112    AssignmentError(Box<AssignmentError>),
113    ReferenceCreationError(Box<SharedValueCreationError>),
114    IntermediateResultWithState(
115        Box<Option<ValueContainer>>,
116        Box<Option<ExecutionLoopState>>,
117    ),
118    ApplyError(Box<ApplyError>),
119    UnauthorizedMove,
120    InvalidMove,
121    MoveToMultipleEndpoints,
122    UnclonableValue, // happens for native rust structs that don't implement clone. FIXME: this should not happen in execution loop
123    StackRestoreMismatch,
124}
125impl ExecutionError {
126    pub fn intermediate_result_with_state(
127        value_opt: Option<ValueContainer>,
128        state_opt: Option<ExecutionLoopState>,
129    ) -> Self {
130        ExecutionError::IntermediateResultWithState(
131            Box::new(value_opt),
132            Box::new(state_opt),
133        )
134    }
135    pub fn invalid_program(error: InvalidProgramError) -> Self {
136        ExecutionError::InvalidProgram(Box::new(error))
137    }
138    pub fn value_error(error: ValueError) -> Self {
139        ExecutionError::ValueError(Box::new(error))
140    }
141    pub fn access_error(error: AccessError) -> Self {
142        ExecutionError::AccessError(Box::new(error))
143    }
144    pub fn update_error(error: UpdateError) -> Self {
145        ExecutionError::UpdateError(Box::new(error))
146    }
147    pub fn cache_value_retrieval_error(
148        error: CacheValueRetrievalError,
149    ) -> Self {
150        ExecutionError::CacheValueRetrievalError(Box::new(error))
151    }
152    pub fn subscriber_error(error: SubscriberError) -> Self {
153        ExecutionError::SubscriberError(Box::new(error))
154    }
155    pub fn response_error(error: ResponseError) -> Self {
156        ExecutionError::ResponseError(Box::new(error))
157    }
158    pub fn illegal_type_error(error: IllegalTypeError) -> Self {
159        ExecutionError::IllegalTypeError(Box::new(error))
160    }
161    pub fn apply_error(error: ApplyError) -> Self {
162        ExecutionError::ApplyError(Box::new(error))
163    }
164    pub fn reference_creation_error(error: SharedValueCreationError) -> Self {
165        ExecutionError::ReferenceCreationError(Box::new(error))
166    }
167    pub fn assignment_error(error: AssignmentError) -> Self {
168        ExecutionError::AssignmentError(Box::new(error))
169    }
170    pub fn dxb_parser_error(error: DXBParserError) -> Self {
171        ExecutionError::DXBParserError(Box::new(error))
172    }
173}
174
175impl From<ApplyError> for ExecutionError {
176    fn from(error: ApplyError) -> Self {
177        ExecutionError::ApplyError(Box::new(error))
178    }
179}
180
181impl From<SharedValueCreationError> for ExecutionError {
182    fn from(error: SharedValueCreationError) -> Self {
183        ExecutionError::ReferenceCreationError(Box::new(error))
184    }
185}
186
187impl From<CacheValueRetrievalError> for ExecutionError {
188    fn from(err: CacheValueRetrievalError) -> Self {
189        ExecutionError::CacheValueRetrievalError(Box::new(err))
190    }
191}
192
193impl From<AccessError> for ExecutionError {
194    fn from(error: AccessError) -> Self {
195        ExecutionError::AccessError(Box::new(error))
196    }
197}
198
199impl From<UpdateError> for ExecutionError {
200    fn from(error: UpdateError) -> Self {
201        ExecutionError::UpdateError(Box::new(error))
202    }
203}
204
205impl From<DXBParserError> for ExecutionError {
206    fn from(error: DXBParserError) -> Self {
207        ExecutionError::DXBParserError(Box::new(error))
208    }
209}
210
211impl From<ValueError> for ExecutionError {
212    fn from(error: ValueError) -> Self {
213        ExecutionError::ValueError(Box::new(error))
214    }
215}
216
217impl From<IllegalTypeError> for ExecutionError {
218    fn from(error: IllegalTypeError) -> Self {
219        ExecutionError::IllegalTypeError(Box::new(error))
220    }
221}
222
223impl From<InvalidProgramError> for ExecutionError {
224    fn from(error: InvalidProgramError) -> Self {
225        ExecutionError::InvalidProgram(Box::new(error))
226    }
227}
228
229impl From<ResponseError> for ExecutionError {
230    fn from(error: ResponseError) -> Self {
231        ExecutionError::ResponseError(Box::new(error))
232    }
233}
234
235impl From<AssignmentError> for ExecutionError {
236    fn from(error: AssignmentError) -> Self {
237        ExecutionError::AssignmentError(Box::new(error))
238    }
239}
240
241impl From<SubscriberError> for ExecutionError {
242    fn from(error: SubscriberError) -> Self {
243        ExecutionError::SubscriberError(Box::new(error))
244    }
245}
246
247impl Display for ExecutionError {
248    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
249        match self {
250            ExecutionError::ReferenceCreationError(err) => {
251                core::write!(f, "Reference from value container error: {err}")
252            }
253            ExecutionError::ApplyError(err) => {
254                core::write!(f, "Apply error: {err}")
255            }
256            ExecutionError::ReferenceNotFound => {
257                core::write!(f, "Reference not found")
258            }
259            ExecutionError::DXBParserError(err) => {
260                core::write!(f, "Parser error: {err}")
261            }
262            ExecutionError::Unknown => {
263                core::write!(f, "Unknown execution error")
264            }
265            ExecutionError::InvalidExecutionState => {
266                core::write!(f, "Invalid execution state")
267            }
268            ExecutionError::ValueError(err) => {
269                core::write!(f, "Value error: {err}")
270            }
271            ExecutionError::SubscriberError(err) => {
272                core::write!(f, "Subscriber error: {err}")
273            }
274            ExecutionError::InvalidProgram(err) => {
275                core::write!(f, "Invalid program error: {err}")
276            }
277            ExecutionError::NotImplemented(msg) => {
278                core::write!(f, "Not implemented: {msg}")
279            }
280            ExecutionError::RequiresAsyncExecution => {
281                core::write!(f, "Program must be executed asynchronously")
282            }
283            ExecutionError::ResponseError(err) => {
284                core::write!(f, "Response error: {err}")
285            }
286            ExecutionError::IllegalTypeError(err) => {
287                core::write!(f, "Illegal type: {err}")
288            }
289            ExecutionError::InvalidUnbox => {
290                core::write!(f, "Tried to unbox a non-reference value")
291            }
292            ExecutionError::AssignmentError(err) => {
293                core::write!(f, "Assignment error: {err}")
294            }
295            ExecutionError::InvalidTypeCast => {
296                core::write!(f, "Invalid type cast")
297            }
298            ExecutionError::ExpectedTypeValue => {
299                core::write!(f, "Expected a type value")
300            }
301            ExecutionError::InvalidSharedValueType => {
302                core::write!(f, "Invalid shared value type")
303            }
304            ExecutionError::AccessError(err) => {
305                core::write!(f, "Access error: {err}")
306            }
307            ExecutionError::MethodNotFound(method_name) => {
308                core::write!(f, "Method not found: {method_name}")
309            }
310            ExecutionError::CacheValueRetrievalError(err) => {
311                core::write!(f, "Cache value retrieval error: {err}")
312            }
313            ExecutionError::IntermediateResultWithState(
314                value_opt,
315                state_opt,
316            ) => {
317                core::write!(
318                    f,
319                    "Execution produced an intermediate result: {:?} with state: {:?}",
320                    value_opt,
321                    state_opt
322                )
323            }
324            ExecutionError::UnauthorizedMove => {
325                core::write!(f, "Unauthorized move of shared pointer")
326            }
327            ExecutionError::InvalidMove => {
328                core::write!(f, "Invalid move of shared pointer")
329            }
330            ExecutionError::MoveToMultipleEndpoints => {
331                core::write!(f, "Illegal move to multiple endpoints")
332            }
333            ExecutionError::UnclonableValue => {
334                core::write!(f, "Tried to clone an unclonable value")
335            }
336            ExecutionError::ExpectedSharedValue => {
337                core::write!(
338                    f,
339                    "Expected a shared value, but got a local value"
340                )
341            }
342            ExecutionError::ExpectedLocalValue => {
343                core::write!(
344                    f,
345                    "Expected a local value, but got a shared value"
346                )
347            }
348            ExecutionError::ExpectedEntityValue => {
349                core::write!(
350                    f,
351                    "Expected an entity value, but got a non-entity value"
352                )
353            }
354            ExecutionError::ExpectedOwnedSharedValue => {
355                core::write!(
356                    f,
357                    "Expected an owned shared value, but got a non-owned shared value"
358                )
359            }
360            ExecutionError::MutableReferenceToNonMutableValue => {
361                core::write!(
362                    f,
363                    "Tried to create a mutable reference to a non-mutable value"
364                )
365            }
366            ExecutionError::StackValueNotAllocated(index) => {
367                core::write!(
368                    f,
369                    "Tried to access unallocated stack value at index {index}"
370                )
371            }
372            ExecutionError::StackOutOfBoundsAccess(index) => {
373                core::write!(
374                    f,
375                    "Tried to access out of bounds stack value at index {index}"
376                )
377            }
378            ExecutionError::InternalSlotDoesNotExist(index) => {
379                core::write!(f, "Internal slot does not exist at index {index}")
380            }
381            ExecutionError::UpdateError(err) => {
382                core::write!(f, "Value update error: {err}")
383            }
384            ExecutionError::Unspecified(msg) => {
385                core::write!(f, "Unspecified error: {msg}")
386            }
387            ExecutionError::StackRestoreMismatch => {
388                core::write!(f, "Stack restore mismatch")
389            }
390        }
391    }
392}