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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//! The `Result` and `Error` types returned by almost everything in rsnek
//!
use std;
use std::result;

use ::api::RtObject;
use ::resources::strings;

pub type RtResult<T> = result::Result<T, Error>;
pub type ObjectResult = RtResult<RtObject>;


pub trait Exception: Sized + std::fmt::Debug + std::fmt::Display {
    fn error_type(&self) -> ErrorType;
    fn message(&self) -> String;
}


#[derive(Debug, Clone, Serialize)]
pub struct Error(pub ErrorType, pub String);


#[derive(Debug, Clone, Serialize)]
pub enum ErrorType {
    Runtime,
    Type,
    Overflow,
    NotImplemented,
    Attribute,
    Value,
    Key,
    ModuleNotFound,
    StopIteration,
    Name,
    System,
    Recursion,
    Assertion,
    Syntax,
    Index
}


impl Error {

    pub fn runtime(message: &str) -> Error {
        Error(ErrorType::Runtime, message.to_string())
    }

    pub fn typerr(message: &str) -> Error {
        Error(ErrorType::Type, message.to_string())
    }

    pub fn overflow(message: &str) -> Error {
        Error(ErrorType::Overflow, message.to_string())
    }

    pub fn not_implemented() -> Error {
        Error(ErrorType::NotImplemented, "Not Implemented".to_string())
    }

    pub fn attribute(message: &str) -> Error {
        Error(ErrorType::Attribute, message.to_string())
    }

    pub fn value(message: &str) -> Self {
        Error(ErrorType::Value, message.to_string())
    }

    pub fn key(message: &str) -> Error {
        Error(ErrorType::Key, message.to_string())
    }

    pub fn module_not_found(name: &str) -> Error {
        Error(ErrorType::ModuleNotFound, format!("No module named '{}'", name))
    }

    pub fn stop_iteration() -> Error {
        return Error(ErrorType::StopIteration, "".to_string())
    }

    pub fn name(name: &str) -> Error {
        Error(ErrorType::Name, format!("name '{}' is not defined", name))
    }

    pub fn system(message: &str) -> Error {
        Error(ErrorType::System, format!("{}, version: {}", message, strings::VERSION))
    }

    pub fn system_not_implemented(name: &str, extra: &str) -> Error {
        Error(ErrorType::System, format!(
            "feature '{}' not implemented for type; {}, version: {}",
            name, extra, strings::VERSION))
    }

    pub fn recursion() -> Error {
        Error(ErrorType::Recursion, "Maximum recursion depth exceeded".to_string())
    }

    pub fn assertion(message: &str) -> Error {
        Error(ErrorType::Assertion, message.to_string())
    }

    pub fn syntax(message: &str) -> Error {
        Error(ErrorType::Syntax, message.to_string())
    }

    pub fn index(message: &str) -> Error {
        Error(ErrorType::Index, message.to_string())
    }


    pub fn log(&self) {
        error!("{:?}Error", self.error_type(); "message" => self.message());
    }
}


impl std::fmt::Display for Error {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{:?}Error: {}", self.error_type(), self.message())
    }
}


impl Exception for Error {
    fn error_type(&self) -> ErrorType {
        self.0.clone()
    }

    fn message(&self) -> String {
        self.1.to_string()
    }

}