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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use spacetimedb_sats::db::error::{AuthError, RelationError};
use spacetimedb_sats::{AlgebraicType, AlgebraicValue};
use std::fmt;
use thiserror::Error;

use crate::expr::SourceId;

#[derive(Error, Debug)]
pub enum ConfigError {
    #[error("Config parameter `{0}` not found.")]
    NotFound(String),
    #[error("Value for config parameter `{0}` is invalid: `{1:?}`. Expected: `{2:?}`")]
    TypeError(String, AlgebraicValue, AlgebraicType),
}

/// Typing Errors
#[derive(Error, Debug)]
pub enum ErrorType {
    #[error("Field should resolve to `bool`, but it got the value `{{0.to_satn()}}`")]
    FieldBool(AlgebraicValue),
    #[error("Error Parsing `{value}` into type [{ty}]: {err}")]
    Parse { value: String, ty: String, err: String },
}

/// Vm Errors
#[derive(Error, Debug)]
pub enum ErrorVm {
    #[error("TypeError {0}")]
    Type(#[from] ErrorType),
    #[error("ErrorLang {0}")]
    Lang(#[from] ErrorLang),
    #[error("RelationError {0}")]
    Rel(#[from] RelationError),
    #[error("AuthError {0}")]
    Auth(#[from] AuthError),
    #[error("Unsupported: {0}")]
    Unsupported(String),
    #[error("No source table with index {0:?}")]
    NoSuchSource(SourceId),
    #[error("ConfigError: {0}")]
    Config(#[from] ConfigError),
    #[error("{0}")]
    Other(#[from] anyhow::Error),
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum ErrorKind {
    Custom(String),
    Compiler,
    TypeMismatch,
    Db,
    Query,
    Duplicated,
    Invalid,
    NotFound,
    Params,
    OutOfBounds,
    Timeout,
    Unauthorized,
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ErrorCtx {
    key: String,
    value: String,
}

impl ErrorCtx {
    pub fn new(key: &str, value: &str) -> Self {
        Self {
            key: key.into(),
            value: value.into(),
        }
    }
}

/// Define the main User Error type for the VM
#[derive(Error, Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ErrorLang {
    pub kind: ErrorKind,
    pub msg: Option<String>,
    /// Optional context for the Error: Which record was not found, what value was invalid, etc.
    pub context: Option<Vec<ErrorCtx>>,
}

impl ErrorLang {
    pub fn new(kind: ErrorKind, msg: Option<&str>) -> Self {
        Self {
            kind,
            msg: msg.map(|x| x.to_string()),
            context: None,
        }
    }

    pub fn with_ctx(self, of: ErrorCtx) -> Self {
        let mut x = self;
        if let Some(ref mut s) = x.context {
            s.push(of)
        } else {
            x.context = Some(vec![of])
        }
        x
    }
}

impl fmt::Display for ErrorLang {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{:?}Error", self.kind)?;
        if let Some(msg) = &self.msg {
            writeln!(f, ": \"{}\"", msg)?;
        }
        if let Some(err) = self.context.as_deref() {
            writeln!(f, " Context:")?;
            for e in err {
                writeln!(f, " {}: {}", e.key, e.value)?;
            }
        }
        Ok(())
    }
}

impl From<ErrorType> for ErrorLang {
    fn from(x: ErrorType) -> Self {
        ErrorLang::new(ErrorKind::TypeMismatch, Some(&x.to_string()))
    }
}

impl From<ErrorVm> for ErrorLang {
    fn from(err: ErrorVm) -> Self {
        match err {
            ErrorVm::Type(err) => err.into(),
            ErrorVm::Other(err) => ErrorLang::new(ErrorKind::Db, Some(&err.to_string())),
            ErrorVm::Rel(err) => ErrorLang::new(ErrorKind::Db, Some(&err.to_string())),
            ErrorVm::Unsupported(err) => ErrorLang::new(ErrorKind::Compiler, Some(&err)),
            ErrorVm::Lang(err) => err,
            ErrorVm::Auth(err) => ErrorLang::new(ErrorKind::Unauthorized, Some(&err.to_string())),
            ErrorVm::Config(err) => ErrorLang::new(ErrorKind::Db, Some(&err.to_string())),
            err @ ErrorVm::NoSuchSource(_) => ErrorLang {
                kind: ErrorKind::Invalid,
                msg: Some(format!("{err:?}")),
                context: None,
            },
        }
    }
}

impl From<RelationError> for ErrorLang {
    fn from(err: RelationError) -> Self {
        ErrorVm::Rel(err).into()
    }
}