notecrumbs

a nostr opengraph server build on nostrdb and egui
git clone git://jb55.com/notecrumbs
Log | Files | Refs | README | LICENSE

error.rs (3309B)


      1 use nostr_sdk::nips::nip19;
      2 use std::array::TryFromSliceError;
      3 use std::fmt;
      4 use tokio::sync::broadcast::error::RecvError;
      5 
      6 #[derive(Debug)]
      7 pub enum Error {
      8     Nip19(nip19::Error),
      9     Http(hyper::http::Error),
     10     Hyper(hyper::Error),
     11     Nostrdb(nostrdb::Error),
     12     NostrClient(nostr_sdk::client::Error),
     13     Recv(RecvError),
     14     Io(std::io::Error),
     15     Generic(String),
     16     Image(image::error::ImageError),
     17     Secp(nostr_sdk::secp256k1::Error),
     18     InvalidUri,
     19     NotFound,
     20     /// Profile picture is too big
     21     TooBig,
     22     InvalidNip19,
     23     InvalidProfilePic,
     24     SliceErr,
     25 }
     26 
     27 impl From<image::error::ImageError> for Error {
     28     fn from(err: image::error::ImageError) -> Self {
     29         Error::Image(err)
     30     }
     31 }
     32 
     33 impl From<http::uri::InvalidUri> for Error {
     34     fn from(err: http::uri::InvalidUri) -> Self {
     35         Error::InvalidUri
     36     }
     37 }
     38 
     39 impl From<nostr_sdk::secp256k1::Error> for Error {
     40     fn from(err: nostr_sdk::secp256k1::Error) -> Self {
     41         Error::Secp(err)
     42     }
     43 }
     44 
     45 impl From<std::io::Error> for Error {
     46     fn from(err: std::io::Error) -> Self {
     47         Error::Io(err)
     48     }
     49 }
     50 
     51 impl From<String> for Error {
     52     fn from(err: String) -> Self {
     53         Error::Generic(err)
     54     }
     55 }
     56 
     57 impl From<RecvError> for Error {
     58     fn from(err: RecvError) -> Self {
     59         Error::Recv(err)
     60     }
     61 }
     62 
     63 impl From<hyper::Error> for Error {
     64     fn from(err: hyper::Error) -> Self {
     65         Error::Hyper(err)
     66     }
     67 }
     68 
     69 impl From<TryFromSliceError> for Error {
     70     fn from(_: TryFromSliceError) -> Self {
     71         Error::SliceErr
     72     }
     73 }
     74 
     75 impl From<nostr_sdk::client::Error> for Error {
     76     fn from(err: nostr_sdk::client::Error) -> Self {
     77         Error::NostrClient(err)
     78     }
     79 }
     80 
     81 impl From<nip19::Error> for Error {
     82     fn from(err: nip19::Error) -> Self {
     83         Error::Nip19(err)
     84     }
     85 }
     86 
     87 impl From<hyper::http::Error> for Error {
     88     fn from(err: hyper::http::Error) -> Self {
     89         Error::Http(err)
     90     }
     91 }
     92 
     93 impl From<nostrdb::Error> for Error {
     94     fn from(err: nostrdb::Error) -> Self {
     95         Error::Nostrdb(err)
     96     }
     97 }
     98 
     99 // Implementing `Display`
    100 impl fmt::Display for Error {
    101     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    102         match self {
    103             Error::Nip19(e) => write!(f, "Nip19 error: {}", e),
    104             Error::Http(e) => write!(f, "HTTP error: {}", e),
    105             Error::Nostrdb(e) => write!(f, "Nostrdb error: {}", e),
    106             Error::NostrClient(e) => write!(f, "Nostr client error: {}", e),
    107             Error::NotFound => write!(f, "Not found"),
    108             Error::Recv(e) => write!(f, "Recieve error: {}", e),
    109             Error::InvalidNip19 => write!(f, "Invalid nip19 object"),
    110             Error::SliceErr => write!(f, "Array slice error"),
    111             Error::TooBig => write!(f, "Profile picture is too big"),
    112             Error::InvalidProfilePic => write!(f, "Profile picture is corrupt"),
    113             Error::Image(err) => write!(f, "Image error: {}", err),
    114             Error::InvalidUri => write!(f, "Invalid url"),
    115             Error::Hyper(err) => write!(f, "Hyper error: {}", err),
    116             Error::Generic(err) => write!(f, "Generic error: {}", err),
    117             Error::Io(err) => write!(f, "Io error: {}", err),
    118             Error::Secp(err) => write!(f, "Signature error: {}", err),
    119         }
    120     }
    121 }
    122 
    123 impl std::error::Error for Error {}