lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README | LICENSE

error.rs (1512B)


      1 use thiserror::Error;
      2 
      3 #[derive(Debug, Error)]
      4 pub enum RadrootsNostrNdbError {
      5     #[error("database path must be utf-8")]
      6     NonUtf8Path,
      7 
      8     #[error("invalid hex for {field}: {reason}")]
      9     InvalidHex { field: &'static str, reason: String },
     10 
     11     #[error("invalid hex length for {field}: expected {expected} bytes, got {actual}")]
     12     InvalidHexLength {
     13         field: &'static str,
     14         expected: usize,
     15         actual: usize,
     16     },
     17 
     18     #[error("event json encode failed: {0}")]
     19     EventJsonEncode(String),
     20 
     21     #[error("nostrdb error: {0}")]
     22     Ndb(String),
     23 }
     24 
     25 #[cfg(feature = "ndb")]
     26 impl From<nostrdb::Error> for RadrootsNostrNdbError {
     27     fn from(value: nostrdb::Error) -> Self {
     28         Self::Ndb(value.to_string())
     29     }
     30 }
     31 
     32 impl From<serde_json::Error> for RadrootsNostrNdbError {
     33     fn from(value: serde_json::Error) -> Self {
     34         Self::EventJsonEncode(value.to_string())
     35     }
     36 }
     37 
     38 #[cfg(test)]
     39 mod tests {
     40     use super::*;
     41 
     42     #[test]
     43     fn converts_nostrdb_error() {
     44         let converted: RadrootsNostrNdbError = nostrdb::Error::NotFound.into();
     45         assert!(converted.to_string().starts_with("nostrdb error:"));
     46     }
     47 
     48     #[test]
     49     fn converts_serde_json_error() {
     50         let source = serde_json::from_str::<serde_json::Value>("not json").expect_err("json error");
     51         let converted: RadrootsNostrNdbError = source.into();
     52         assert!(
     53             converted
     54                 .to_string()
     55                 .starts_with("event json encode failed:")
     56         );
     57     }
     58 }