indradb_plugin_host/
errors.rs

1use std::error::Error as StdError;
2use std::fmt;
3
4use indradb::Error as IndraDBError;
5use serde_json::Error as JsonError;
6
7/// A plugin error.
8#[non_exhaustive]
9#[derive(Debug)]
10pub enum Error {
11    /// Json (de-)serialization error.
12    Json(JsonError),
13    /// IndraDB error.
14    IndraDB(IndraDBError),
15    // When the input argument is valid JSON, but invalid for plugin-specific
16    // reasons.
17    InvalidArgument(String),
18    /// Any other kind of error.
19    Other(Box<dyn StdError + Send + Sync>),
20}
21
22impl StdError for Error {
23    fn source(&self) -> Option<&(dyn StdError + 'static)> {
24        match *self {
25            Error::Json(ref err) => Some(err),
26            Error::IndraDB(ref err) => Some(err),
27            Error::Other(ref err) => Some(&**err),
28            _ => None,
29        }
30    }
31}
32
33impl fmt::Display for Error {
34    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
35        match *self {
36            Error::Json(ref err) => write!(f, "json error: {err}"),
37            Error::IndraDB(ref err) => write!(f, "IndraDB error: {err}"),
38            Error::InvalidArgument(ref msg) => write!(f, "{msg}"),
39            Error::Other(ref err) => write!(f, "{err}"),
40        }
41    }
42}
43
44impl From<JsonError> for Error {
45    fn from(err: JsonError) -> Self {
46        Error::Json(err)
47    }
48}
49
50impl From<IndraDBError> for Error {
51    fn from(err: IndraDBError) -> Self {
52        Error::IndraDB(err)
53    }
54}
55
56impl From<Box<dyn StdError + Send + Sync>> for Error {
57    fn from(err: Box<dyn StdError + Send + Sync>) -> Self {
58        Error::Other(err)
59    }
60}