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
use std::error::Error;
use std::fmt;
use std::fmt::{Display, Formatter};

/// A property that splits exceptions into 2 groups
///
/// `Static` exceptions can be detected before code execution.
/// These include errors in the format of bytecode, encoding errors etc.
/// The rest of exceptions are `Runtime` which means that invalid code can lead to such exceptions.
///
#[derive(Debug)]
pub enum ExceptionType {
    Static,
    Runtime,
}

impl Display for ExceptionType {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            ExceptionType::Runtime => write!(f, "Runtime"),
            ExceptionType::Static => write!(f, "Syntax"),
        }
    }
}

/// An error that is raised by the VM
///
/// Exception contains a type (`exception_type`), a name and a message that are displayed
/// with the stacktrace to the user.
#[derive(Debug)]
pub struct Exception {
    pub exception_type: ExceptionType,
    pub name: String,
    pub message: String,
}

impl Display for Exception {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "[{}] {}: {}",
            self.exception_type, self.name, self.message
        )
    }
}

impl Error for Exception {}