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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#![deny(unsafe_code, warnings, rust_2018_idioms)]
#![allow(clippy::derive_partial_eq_without_eq)]

mod panic_hook;

pub mod common;
#[cfg(feature = "sql")]
pub mod quaint;
pub mod query_engine;
pub mod schema_engine;

use serde::{Deserialize, Serialize};
use std::borrow::Cow;

pub use panic_hook::set_panic_hook;

pub trait UserFacingError: serde::Serialize {
    const ERROR_CODE: &'static str;

    fn message(&self) -> String;
}

/// A less dynamic type of user-facing errors. This is used in the introspection and migration
/// engines for simpler, more robust and helpful error handling — extra details are attached
/// opportunistically.
pub trait SimpleUserFacingError {
    const ERROR_CODE: &'static str;
    const MESSAGE: &'static str;
}

impl<T> UserFacingError for T
where
    T: SimpleUserFacingError + Serialize,
{
    const ERROR_CODE: &'static str = <Self as SimpleUserFacingError>::ERROR_CODE;

    fn message(&self) -> String {
        <Self as SimpleUserFacingError>::MESSAGE.to_owned()
    }
}

#[derive(Serialize, Deserialize, Clone, PartialEq, Debug)]
pub struct KnownError {
    pub message: String,
    pub meta: serde_json::Value,
    pub error_code: Cow<'static, str>,
}

impl KnownError {
    pub fn new<T: UserFacingError>(inner: T) -> KnownError {
        KnownError {
            message: inner.message(),
            meta: serde_json::to_value(&inner).expect("Failed to render user facing error metadata to JSON"),
            error_code: Cow::from(T::ERROR_CODE),
        }
    }
}

#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct UnknownError {
    pub message: String,
    pub backtrace: Option<String>,
}

impl UnknownError {
    pub fn new(err: &dyn std::error::Error) -> Self {
        UnknownError {
            message: err.to_string(),
            backtrace: None,
        }
    }
}

#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
pub struct Error {
    is_panic: bool,
    #[serde(flatten)]
    inner: ErrorType,

    #[serde(skip_serializing_if = "Option::is_none")]
    batch_request_idx: Option<usize>,
}

#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
#[serde(untagged)]
enum ErrorType {
    Known(KnownError),
    Unknown(UnknownError),
}

impl Error {
    /// Try to interpret the error as a known error.
    pub fn as_known(&self) -> Option<&KnownError> {
        match &self.inner {
            ErrorType::Known(err) => Some(err),
            ErrorType::Unknown(_) => None,
        }
    }

    pub fn message(&self) -> &str {
        match &self.inner {
            ErrorType::Known(err) => &err.message,
            ErrorType::Unknown(err) => &err.message,
        }
    }

    pub fn batch_request_idx(&self) -> Option<usize> {
        self.batch_request_idx
    }

    pub fn new_non_panic_with_current_backtrace(message: String) -> Self {
        Error {
            inner: ErrorType::Unknown(UnknownError {
                message,
                backtrace: Some(format!("{:?}", backtrace::Backtrace::new())),
            }),
            is_panic: false,
            batch_request_idx: None,
        }
    }

    /// Construct a new UnknownError from a `PanicInfo` in a panic hook. `UnknownError`s created
    /// with this constructor will have a proper, useful backtrace.
    pub fn new_in_panic_hook(panic_info: &std::panic::PanicInfo<'_>) -> Self {
        let message = panic_info
            .payload()
            .downcast_ref::<&str>()
            .map(|s| -> String { (*s).to_owned() })
            .or_else(|| panic_info.payload().downcast_ref::<String>().map(|s| s.to_owned()))
            .unwrap_or_else(|| "<unknown panic>".to_owned());

        let backtrace = Some(format!("{:?}", backtrace::Backtrace::new()));
        let location = panic_info
            .location()
            .map(|loc| format!("{loc}"))
            .unwrap_or_else(|| "<unknown location>".to_owned());

        Error {
            inner: ErrorType::Unknown(UnknownError {
                message: format!("[{location}] {message}"),
                backtrace,
            }),
            is_panic: true,
            batch_request_idx: None,
        }
    }

    /// Build from a KnownError
    pub fn new_known(err: KnownError) -> Self {
        Error {
            inner: ErrorType::Known(err),
            is_panic: false,
            batch_request_idx: None,
        }
    }

    pub fn from_panic_payload(panic_payload: Box<dyn std::any::Any + Send + 'static>) -> Self {
        let message = Self::extract_panic_message(panic_payload).unwrap_or_else(|| "<unknown panic>".to_owned());

        Error {
            inner: ErrorType::Unknown(UnknownError {
                message,
                backtrace: None,
            }),
            is_panic: true,
            batch_request_idx: None,
        }
    }

    pub fn extract_panic_message(panic_payload: Box<dyn std::any::Any + Send + 'static>) -> Option<String> {
        panic_payload
            .downcast_ref::<&str>()
            .map(|s| -> String { (*s).to_owned() })
            .or_else(|| panic_payload.downcast_ref::<String>().map(|s| s.to_owned()))
    }

    /// Extract the inner known error, or panic.
    pub fn unwrap_known(self) -> KnownError {
        match self.inner {
            ErrorType::Known(err) => err,
            err @ ErrorType::Unknown(_) => panic!("Expected known error, got {err:?}"),
        }
    }

    pub fn set_batch_request_idx(&mut self, batch_request_idx: usize) {
        self.batch_request_idx = Some(batch_request_idx)
    }
}

pub fn new_backtrace() -> backtrace::Backtrace {
    backtrace::Backtrace::new()
}

impl<T: UserFacingError> From<T> for Error {
    fn from(err: T) -> Self {
        Self::from(KnownError::new(err))
    }
}

impl From<UnknownError> for Error {
    fn from(unknown_error: UnknownError) -> Self {
        Error {
            inner: ErrorType::Unknown(unknown_error),
            is_panic: false,
            batch_request_idx: None,
        }
    }
}

impl From<KnownError> for Error {
    fn from(known_error: KnownError) -> Self {
        Error {
            is_panic: false,
            inner: ErrorType::Known(known_error),
            batch_request_idx: None,
        }
    }
}