Files
leonwww/protocol/library/src/error.rs
2025-09-26 14:05:51 +01:00

74 lines
1.8 KiB
Rust

use std::fmt;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum GurtError {
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("Serialization error: {0}")]
Serialization(#[from] serde_json::Error),
#[error("Cryptographic error: {0}")]
Crypto(String),
#[error("Protocol error: {0}")]
Protocol(String),
#[error("Invalid message format: {0}")]
InvalidMessage(String),
#[error("Connection error: {0}")]
Connection(String),
#[error("Handshake failed: {0}")]
Handshake(String),
#[error("Timeout error: {0}")]
Timeout(String),
#[error("Server error: {status} {message}")]
Server { status: u16, message: String },
#[error("Client error: {0}")]
Client(String),
#[error("Cancelled")]
Cancelled,
}
pub type Result<T> = std::result::Result<T, GurtError>;
impl GurtError {
pub fn crypto<T: fmt::Display>(msg: T) -> Self {
GurtError::Crypto(msg.to_string())
}
pub fn protocol<T: fmt::Display>(msg: T) -> Self {
GurtError::Protocol(msg.to_string())
}
pub fn invalid_message<T: fmt::Display>(msg: T) -> Self {
GurtError::InvalidMessage(msg.to_string())
}
pub fn connection<T: fmt::Display>(msg: T) -> Self {
GurtError::Connection(msg.to_string())
}
pub fn handshake<T: fmt::Display>(msg: T) -> Self {
GurtError::Handshake(msg.to_string())
}
pub fn timeout<T: fmt::Display>(msg: T) -> Self {
GurtError::Timeout(msg.to_string())
}
pub fn server(status: u16, message: String) -> Self {
GurtError::Server { status, message }
}
pub fn client<T: fmt::Display>(msg: T) -> Self {
GurtError::Client(msg.to_string())
}
}