nostr-rs-relay

My dev fork of nostr-rs-relay
git clone git://jb55.com/nostr-rs-relay
Log | Files | Refs | README | LICENSE

error.rs (3000B)


      1 //! Error handling
      2 use std::result;
      3 use thiserror::Error;
      4 use tungstenite::error::Error as WsError;
      5 
      6 /// Simple `Result` type for errors in this module
      7 pub type Result<T, E = Error> = result::Result<T, E>;
      8 
      9 /// Custom error type for Nostr
     10 #[derive(Error, Debug)]
     11 pub enum Error {
     12     #[error("Protocol parse error")]
     13     ProtoParseError,
     14     #[error("Connection error")]
     15     ConnError,
     16     #[error("Client write error")]
     17     ConnWriteError,
     18     #[error("EVENT parse failed")]
     19     EventParseFailed,
     20     #[error("ClOSE message parse failed")]
     21     CloseParseFailed,
     22     #[error("Event validation failed")]
     23     EventInvalid,
     24     #[error("Event too large")]
     25     EventMaxLengthError(usize),
     26     #[error("Subscription identifier max length exceeded")]
     27     SubIdMaxLengthError,
     28     #[error("Maximum concurrent subscription count reached")]
     29     SubMaxExceededError,
     30     // this should be used if the JSON is invalid
     31     #[error("JSON parsing failed")]
     32     JsonParseFailed(serde_json::Error),
     33     #[error("WebSocket proto error")]
     34     WebsocketError(WsError),
     35     #[error("Command unknown")]
     36     CommandUnknownError,
     37     #[error("SQL error")]
     38     SqlError(rusqlite::Error),
     39     #[error("Config error")]
     40     ConfigError(config::ConfigError),
     41     #[error("Data directory does not exist")]
     42     DatabaseDirError,
     43     #[error("Database Connection Pool Error")]
     44     DatabasePoolError(r2d2::Error),
     45     #[error("Custom Error : {0}")]
     46     CustomError(String),
     47     #[error("Task join error")]
     48     JoinError,
     49     #[error("Hyper Client error")]
     50     HyperError(hyper::Error),
     51     #[error("Hex encoding error")]
     52     HexError(hex::FromHexError),
     53     #[error("Delegation parse error")]
     54     DelegationParseError,
     55     #[error("Unknown/Undocumented")]
     56     UnknownError,
     57 }
     58 
     59 //impl From<Box<dyn std::error::Error>> for Error {
     60 //    fn from(e: Box<dyn std::error::Error>) -> Self {
     61 //        Error::CustomError("error".to_owned())
     62 //    }
     63 //}
     64 
     65 impl From<hex::FromHexError> for Error {
     66     fn from(h: hex::FromHexError) -> Self {
     67         Error::HexError(h)
     68     }
     69 }
     70 
     71 impl From<hyper::Error> for Error {
     72     fn from(h: hyper::Error) -> Self {
     73         Error::HyperError(h)
     74     }
     75 }
     76 
     77 impl From<r2d2::Error> for Error {
     78     fn from(d: r2d2::Error) -> Self {
     79         Error::DatabasePoolError(d)
     80     }
     81 }
     82 
     83 impl From<tokio::task::JoinError> for Error {
     84     /// Wrap SQL error
     85     fn from(_j: tokio::task::JoinError) -> Self {
     86         Error::JoinError
     87     }
     88 }
     89 
     90 impl From<rusqlite::Error> for Error {
     91     /// Wrap SQL error
     92     fn from(r: rusqlite::Error) -> Self {
     93         Error::SqlError(r)
     94     }
     95 }
     96 
     97 impl From<serde_json::Error> for Error {
     98     /// Wrap JSON error
     99     fn from(r: serde_json::Error) -> Self {
    100         Error::JsonParseFailed(r)
    101     }
    102 }
    103 
    104 impl From<WsError> for Error {
    105     /// Wrap Websocket error
    106     fn from(r: WsError) -> Self {
    107         Error::WebsocketError(r)
    108     }
    109 }
    110 
    111 impl From<config::ConfigError> for Error {
    112     /// Wrap Config error
    113     fn from(r: config::ConfigError) -> Self {
    114         Error::ConfigError(r)
    115     }
    116 }