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