tci/runtime/
error.rs

1use super::types::*;
2use crate::filedb::*;
3use crate::util::*;
4use core::fmt;
5
6pub fn render_err(error: &IError, stack_trace: &Vec<CallFrame>, files: &FileDb) -> String {
7    let mut out = String::new();
8
9    write!(out, "{}: {}\n", error.short_name, error.message).unwrap();
10
11    for frame in stack_trace.iter().skip(1) {
12        files.write_loc(&mut out, frame.loc).unwrap();
13        write!(out, "\n").unwrap();
14        files.display_loc(&mut out, frame.loc).unwrap();
15    }
16
17    return out;
18}
19
20#[derive(Debug, Clone, serde::Serialize)]
21pub struct IError {
22    pub short_name: String,
23    pub message: String,
24}
25
26impl IError {
27    pub fn new(short_name: String, message: String) -> Self {
28        Self {
29            short_name,
30            message,
31        }
32    }
33}
34
35#[allow(unused_macros)]
36macro_rules! ierror {
37    ($arg1:tt,$($arg:tt)*) => {
38        IError::new($arg1.to_string(), format!($($arg)*))
39    };
40}
41
42#[allow(unused_macros)]
43macro_rules! ierr {
44    ($arg1:tt,$($arg:tt)*) => {
45        Err(IError::new($arg1.to_string(), format!($($arg)*)))
46    };
47}
48
49impl From<fmt::Error> for IError {
50    fn from(err: fmt::Error) -> Self {
51        ierror!("WriteFailed", "failed to write to output ({})", err)
52    }
53}