1use std::fmt;
4
5#[derive(Debug)]
7pub enum Error {
8 Sqlite(rusqlite::Error),
10 SchemaTooNew {
12 found: i64,
14 supported: i64,
16 },
17 InvalidTimestamp(String),
19 Json(serde_json::Error),
21 Search(omgbase_search::Error),
23 Mutation(omgbase_mutate::MutationError),
25 Other(String),
27}
28
29impl fmt::Display for Error {
30 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31 match self {
32 Error::Sqlite(e) => write!(f, "sqlite: {e}"),
33 Error::SchemaTooNew { found, supported } => write!(
34 f,
35 "database schema (v{found}) is newer than this build (v{supported}); upgrade omgbase"
36 ),
37 Error::InvalidTimestamp(ts) => write!(f, "invalid RFC 3339 UTC timestamp {ts:?}"),
38 Error::Json(e) => write!(f, "json: {e}"),
39 Error::Search(e) => write!(f, "search: {e}"),
40 Error::Mutation(e) => write!(f, "mutation: {e}"),
41 Error::Other(msg) => f.write_str(msg),
42 }
43 }
44}
45
46impl std::error::Error for Error {
47 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
48 match self {
49 Error::Sqlite(e) => Some(e),
50 Error::Json(e) => Some(e),
51 Error::Search(e) => Some(e),
52 Error::Mutation(e) => Some(e),
53 _ => None,
54 }
55 }
56}
57
58impl From<rusqlite::Error> for Error {
59 fn from(e: rusqlite::Error) -> Self {
60 Error::Sqlite(e)
61 }
62}
63
64impl From<serde_json::Error> for Error {
65 fn from(e: serde_json::Error) -> Self {
66 Error::Json(e)
67 }
68}
69
70impl From<omgbase_search::Error> for Error {
71 fn from(e: omgbase_search::Error) -> Self {
72 Error::Search(e)
73 }
74}
75
76impl From<omgbase_mutate::MutationError> for Error {
77 fn from(e: omgbase_mutate::MutationError) -> Self {
78 Error::Mutation(e)
79 }
80}
81
82impl Error {
83 #[must_use]
85 pub fn as_mutation(&self) -> Option<&omgbase_mutate::MutationError> {
86 match self {
87 Error::Mutation(e) => Some(e),
88 _ => None,
89 }
90 }
91}
92
93pub type Result<T> = std::result::Result<T, Error>;