use std::{fmt, io};
use thiserror::Error;
use crate::proto::serialize::txt::ParseError;
#[cfg(feature = "backtrace")]
use crate::proto::{ExtBacktrace, trace};
use crate::proto::{ProtoError, ProtoErrorKind};
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum PersistenceErrorKind {
#[error("error recovering from journal: {}", _0)]
Recovery(&'static str),
#[error("wrong insert count: {} expect: {}", got, expect)]
WrongInsertCount {
got: usize,
expect: usize,
},
#[error("proto error: {0}")]
Proto(#[from] ProtoError),
#[cfg(feature = "sqlite")]
#[error("sqlite error: {0}")]
Sqlite(#[from] rusqlite::Error),
#[error("request timed out")]
Timeout,
}
#[derive(Debug, Error)]
pub struct PersistenceError {
kind: PersistenceErrorKind,
#[cfg(feature = "backtrace")]
backtrack: Option<ExtBacktrace>,
}
impl PersistenceError {
pub fn kind(&self) -> &PersistenceErrorKind {
&self.kind
}
}
impl fmt::Display for PersistenceError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
cfg_if::cfg_if! {
if #[cfg(feature = "backtrace")] {
if let Some(backtrace) = &self.backtrack {
fmt::Display::fmt(&self.kind, f)?;
fmt::Debug::fmt(backtrace, f)
} else {
fmt::Display::fmt(&self.kind, f)
}
} else {
fmt::Display::fmt(&self.kind, f)
}
}
}
}
impl From<PersistenceErrorKind> for PersistenceError {
fn from(kind: PersistenceErrorKind) -> Self {
Self {
kind,
#[cfg(feature = "backtrace")]
backtrack: trace!(),
}
}
}
impl From<ProtoError> for PersistenceError {
fn from(e: ProtoError) -> Self {
match e.kind() {
ProtoErrorKind::Timeout => PersistenceErrorKind::Timeout.into(),
_ => PersistenceErrorKind::from(e).into(),
}
}
}
#[cfg(feature = "sqlite")]
impl From<rusqlite::Error> for PersistenceError {
fn from(e: rusqlite::Error) -> Self {
PersistenceErrorKind::from(e).into()
}
}
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum ConfigErrorKind {
#[error("io error: {0}")]
Io(#[from] io::Error),
#[cfg(feature = "toml")]
#[error("toml decode error: {0}")]
TomlDecode(#[from] toml::de::Error),
#[error("failed to parse the zone file: {0}")]
ZoneParse(#[from] ParseError),
}
#[derive(Debug)]
pub struct ConfigError {
kind: Box<ConfigErrorKind>,
#[cfg(feature = "backtrace")]
backtrack: Option<ExtBacktrace>,
}
impl ConfigError {
pub fn kind(&self) -> &ConfigErrorKind {
&self.kind
}
}
impl fmt::Display for ConfigError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
cfg_if::cfg_if! {
if #[cfg(feature = "backtrace")] {
if let Some(backtrace) = &self.backtrack {
fmt::Display::fmt(&self.kind, f)?;
fmt::Debug::fmt(backtrace, f)
} else {
fmt::Display::fmt(&self.kind, f)
}
} else {
fmt::Display::fmt(&self.kind, f)
}
}
}
}
impl<E> From<E> for ConfigError
where
E: Into<ConfigErrorKind>,
{
fn from(error: E) -> Self {
let kind: ConfigErrorKind = error.into();
Self {
kind: Box::new(kind),
#[cfg(feature = "backtrace")]
backtrack: trace!(),
}
}
}