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 MintExhausted {
28 prefix: String,
30 rejected: usize,
32 },
33 InvalidEffect(String),
37 Other(String),
39}
40
41impl fmt::Display for Error {
42 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43 match self {
44 Error::Sqlite(e) => write!(f, "sqlite: {e}"),
45 Error::SchemaTooNew { found, supported } => write!(
46 f,
47 "database schema (v{found}) is newer than this build (v{supported}); upgrade omgbase"
48 ),
49 Error::InvalidTimestamp(ts) => write!(f, "invalid RFC 3339 UTC timestamp {ts:?}"),
50 Error::Json(e) => write!(f, "json: {e}"),
51 Error::Search(e) => write!(f, "search: {e}"),
52 Error::Mutation(e) => write!(f, "mutation: {e}"),
53 Error::MintExhausted { prefix, rejected } => write!(
54 f,
55 "mint({prefix}): {rejected} consecutive candidates were already in use; the minter cannot produce a fresh id"
56 ),
57 Error::InvalidEffect(msg) => write!(f, "invalid effect: {msg}"),
58 Error::Other(msg) => f.write_str(msg),
59 }
60 }
61}
62
63impl std::error::Error for Error {
64 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
65 match self {
66 Error::Sqlite(e) => Some(e),
67 Error::Json(e) => Some(e),
68 Error::Search(e) => Some(e),
69 Error::Mutation(e) => Some(e),
70 _ => None,
71 }
72 }
73}
74
75impl From<rusqlite::Error> for Error {
76 fn from(e: rusqlite::Error) -> Self {
77 Error::Sqlite(e)
78 }
79}
80
81impl From<serde_json::Error> for Error {
82 fn from(e: serde_json::Error) -> Self {
83 Error::Json(e)
84 }
85}
86
87impl From<omgbase_search::Error> for Error {
88 fn from(e: omgbase_search::Error) -> Self {
89 Error::Search(e)
90 }
91}
92
93impl From<omgbase_mutate::MutationError> for Error {
94 fn from(e: omgbase_mutate::MutationError) -> Self {
95 Error::Mutation(e)
96 }
97}
98
99impl Error {
100 #[must_use]
102 pub fn as_mutation(&self) -> Option<&omgbase_mutate::MutationError> {
103 match self {
104 Error::Mutation(e) => Some(e),
105 _ => None,
106 }
107 }
108}
109
110pub type Result<T> = std::result::Result<T, Error>;