use thiserror::Error;
use tor_netdoc::parse2::VerifyFailed;
pub(crate) trait IsFatal: std::error::Error {
fn is_fatal(&self) -> bool;
}
#[derive(Debug, Error)]
#[non_exhaustive]
pub(crate) enum AuthorityRequestError {
#[error("tcp connection error: {0}")]
TcpConnect(std::io::Error),
#[error("dirclient error: {0}")]
Request(Box<tor_dirclient::RequestFailedError>),
#[error("response error: {0}")]
Response(&'static str),
#[error("internal error")]
Bug(#[from] tor_error::Bug),
}
impl From<tor_dirclient::RequestFailedError> for AuthorityRequestError {
fn from(value: tor_dirclient::RequestFailedError) -> Self {
Self::Request(Box::new(value))
}
}
impl IsFatal for AuthorityRequestError {
fn is_fatal(&self) -> bool {
matches!(&self, Self::Bug(_))
}
}
#[derive(Debug, Error)]
#[non_exhaustive]
pub(crate) enum DatabaseError {
#[error("low-level rusqlite error: {0}")]
LowLevel(#[from] rusqlite::Error),
#[error("incompatible schema version: {version}")]
IncompatibleSchema {
version: String,
},
#[error("pool error: {0}")]
Pool(#[from] r2d2::Error),
#[error("Internal error")]
Bug(#[from] tor_error::Bug),
}
#[derive(Debug, Error)]
#[non_exhaustive]
pub(crate) enum OperationError {
#[error("authority request error: {0}")]
AuthorityRequest(#[from] Box<AuthorityRequestError>),
#[error("database error: {0}")]
Database(#[from] DatabaseError),
#[error("verification error: {0}")]
VerifyFailed(#[from] VerifyFailed),
#[error("Internal error")]
Bug(#[from] tor_error::Bug),
}
impl From<AuthorityRequestError> for OperationError {
fn from(value: AuthorityRequestError) -> Self {
Self::AuthorityRequest(Box::new(value))
}
}
impl IsFatal for OperationError {
fn is_fatal(&self) -> bool {
matches!(&self, Self::Database(_) | Self::Bug(_))
}
}