2021-12-11 21:43:41 -06:00
|
|
|
//! Error handling
|
2021-12-05 16:53:26 -06:00
|
|
|
use std::result;
|
|
|
|
use thiserror::Error;
|
|
|
|
use tungstenite::error::Error as WsError;
|
|
|
|
|
2021-12-11 21:43:41 -06:00
|
|
|
/// Simple `Result` type for errors in this module
|
2021-12-05 16:53:26 -06:00
|
|
|
pub type Result<T, E = Error> = result::Result<T, E>;
|
|
|
|
|
2021-12-11 21:43:41 -06:00
|
|
|
/// Custom error type for Nostr
|
2021-12-05 16:53:26 -06:00
|
|
|
#[derive(Error, Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
#[error("Protocol parse error")]
|
|
|
|
ProtoParseError,
|
|
|
|
#[error("Connection error")]
|
|
|
|
ConnError,
|
|
|
|
#[error("Client write error")]
|
|
|
|
ConnWriteError,
|
2021-12-05 17:33:40 -06:00
|
|
|
#[error("EVENT parse failed")]
|
2021-12-05 16:53:26 -06:00
|
|
|
EventParseFailed,
|
2021-12-05 17:33:40 -06:00
|
|
|
#[error("ClOSE message parse failed")]
|
|
|
|
CloseParseFailed,
|
2021-12-05 16:53:26 -06:00
|
|
|
#[error("Event validation failed")]
|
|
|
|
EventInvalid,
|
2021-12-05 17:33:40 -06:00
|
|
|
// this should be used if the JSON is invalid
|
2021-12-05 16:53:26 -06:00
|
|
|
#[error("JSON parsing failed")]
|
|
|
|
JsonParseFailed(serde_json::Error),
|
|
|
|
#[error("WebSocket proto error")]
|
|
|
|
WebsocketError(WsError),
|
|
|
|
#[error("Command unknown")]
|
|
|
|
CommandUnknownError,
|
2021-12-11 15:48:59 -06:00
|
|
|
#[error("SQL error")]
|
|
|
|
SqlError(rusqlite::Error),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<rusqlite::Error> for Error {
|
2021-12-11 21:43:41 -06:00
|
|
|
/// Wrap SQL error
|
2021-12-11 15:48:59 -06:00
|
|
|
fn from(r: rusqlite::Error) -> Self {
|
|
|
|
Error::SqlError(r)
|
|
|
|
}
|
2021-12-05 16:53:26 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<serde_json::Error> for Error {
|
2021-12-11 21:43:41 -06:00
|
|
|
/// Wrap JSON error
|
2021-12-05 16:53:26 -06:00
|
|
|
fn from(r: serde_json::Error) -> Self {
|
|
|
|
Error::JsonParseFailed(r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<WsError> for Error {
|
2021-12-11 21:43:41 -06:00
|
|
|
/// Wrap Websocket error
|
2021-12-05 16:53:26 -06:00
|
|
|
fn from(r: WsError) -> Self {
|
|
|
|
Error::WebsocketError(r)
|
|
|
|
}
|
|
|
|
}
|