use serde::Serialize;
use thiserror::Error;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Error, Serialize)]
pub enum Error {
#[error("Not connected to Centrifugo server")]
NotConnected,
#[error("Serialization error: {0}")]
SerializationError(String),
#[error("Centrifugo error: {0}")]
CentrifugoError(String),
#[error("Base64 error: {0}")]
Base64Error(String),
#[error("Request error: {0}")]
RequestError(String),
#[error("Not supported: {0}")]
Unsupported(&'static str),
}
impl From<serde_json::Error> for Error {
fn from(err: serde_json::Error) -> Self {
Error::SerializationError(err.to_string())
}
}
impl From<base64::DecodeError> for Error {
fn from(err: base64::DecodeError) -> Self {
Error::Base64Error(err.to_string())
}
}
impl From<centrifuge::Error> for Error {
fn from(err: centrifuge::Error) -> Self {
match err {
centrifuge::Error::ClientDisconnected | centrifuge::Error::ClientClosed => {
Error::NotConnected
}
centrifuge::Error::Server(e) => Error::CentrifugoError(e.to_string()),
e => Error::RequestError(e.to_string()),
}
}
}