-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.rs
More file actions
79 lines (73 loc) · 2.56 KB
/
Copy patherror.rs
File metadata and controls
79 lines (73 loc) · 2.56 KB
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
use serde::{Deserialize, Serialize};
use std::fmt;
use techscript_common::Span;
use techscript_errors::ErrorCode;
/// Detailed categories for execution failures.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum RuntimeErrorKind {
UndefinedVariable(String),
DivisionByZero,
IndexOutOfBounds,
InvalidCast(String),
AssertionFailed(String),
TypeMismatch { expected: String, found: String },
InvalidOperation(String),
StackOverflow,
MemberNotFound(String),
ArityMismatch { expected: usize, found: usize },
UserError(String),
}
impl fmt::Display for RuntimeErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UndefinedVariable(name) => write!(f, "Undefined variable '{}'", name),
Self::DivisionByZero => write!(f, "division by zero"),
Self::IndexOutOfBounds => write!(f, "Index out of bounds"),
Self::InvalidCast(msg) => write!(f, "Invalid cast: {}", msg),
Self::AssertionFailed(msg) => write!(f, "Assertion failed: {}", msg),
Self::TypeMismatch { expected, found } => {
write!(
f,
"Type mismatch: expected '{}', found '{}'",
expected, found
)
}
Self::InvalidOperation(msg) => write!(f, "Invalid operation: {}", msg),
Self::StackOverflow => write!(f, "Stack overflow"),
Self::MemberNotFound(name) => write!(f, "Member '{}' not found", name),
Self::ArityMismatch { expected, found } => {
write!(
f,
"Arity mismatch: expected {} arguments, found {}",
expected, found
)
}
Self::UserError(msg) => write!(f, "{}", msg),
}
}
}
/// Standardized RuntimeError context holding code categorizations.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RuntimeError {
pub kind: RuntimeErrorKind,
pub span: Option<Span>,
pub code: Option<ErrorCode>,
pub message: String,
}
impl RuntimeError {
pub fn new(kind: RuntimeErrorKind, span: Option<Span>, code: Option<ErrorCode>) -> Self {
let message = kind.to_string();
Self {
kind,
span,
code,
message,
}
}
}
impl fmt::Display for RuntimeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Runtime Error: {}", self.message)
}
}
impl std::error::Error for RuntimeError {}