1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use std::{error::Error, fmt::Display};
use crate::{IndexError, KeyError, ModuleLoadError, SliceError, ValueError};
use super::{resolver::SelectorError};
pub type RuntimeResult<T> = Result<T, RuntimeError>;
#[derive(Debug)]
pub struct RuntimeError {
pub error_type: RuntimeErrorType,
pub description: String,
pub stack_trace: Option<String>,
}
impl Error for RuntimeError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match &self.error_type {
RuntimeErrorType::IndexError(err) => Some(err),
RuntimeErrorType::KeyError(err) => Some(err),
RuntimeErrorType::ValueError(err) => Some(err),
_ => None,
}
}
fn cause(&self) -> Option<&dyn Error> {
self.source()
}
}
impl Display for RuntimeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "[{}] {}", self.error_type, self.description)
}
}
#[derive(Debug)]
pub enum RuntimeErrorType {
StackOverflow,
StackUnderflow,
InvalidAccess,
InvalidOperation,
InternalError,
ArgumentMismatch,
ArgumentError,
CannotInvokeValue,
AssertError,
TypeError,
ValueError(ValueError),
IndexError(IndexError),
KeyError(KeyError),
SliceError(SliceError),
SelectorError(SelectorError),
ModuleLoadError(ModuleLoadError),
UserError,
ControlFlowError,
}
impl Display for RuntimeErrorType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", match self {
RuntimeErrorType::StackOverflow => "STACK_OVERFLOW_ERROR",
RuntimeErrorType::StackUnderflow => "STACK_UNDERFLOW_ERROR",
RuntimeErrorType::InvalidAccess => "INVALID_ACCESS_ERROR",
RuntimeErrorType::InvalidOperation => "INVALID_OP_ERROR",
RuntimeErrorType::InternalError => "EXTERNAL_ERROR",
RuntimeErrorType::ArgumentMismatch => "ARG_MISMATCH_ERROR",
RuntimeErrorType::ArgumentError => "ARG_ERROR",
RuntimeErrorType::CannotInvokeValue => "INVOKE_ERROR",
RuntimeErrorType::UserError => "USER_ERROR",
RuntimeErrorType::AssertError => "ASSERT_ERROR",
RuntimeErrorType::TypeError => "TYPE_ERROR",
RuntimeErrorType::ValueError(_) => "VALUE_ERROR",
RuntimeErrorType::IndexError(_) => "INDEX_ERROR",
RuntimeErrorType::KeyError(_) => "KEY_ERROR",
RuntimeErrorType::SliceError(_) => "SLICE_ERROR",
RuntimeErrorType::SelectorError(_) => "ERR_SELECTOR_ERROR",
RuntimeErrorType::ModuleLoadError(_) => "ERR_MODULE_ERROR",
RuntimeErrorType::ControlFlowError => "CONTROL_FLOW_ERROR",
})
}
}
pub(crate) trait IntoRuntimeResult<T> {
fn into_runtime_result(self) -> RuntimeResult<T>;
}