trust_dns_server/error/
persistence_error.rs1use std::fmt;
9
10use crate::proto::error::*;
11use thiserror::Error;
12
13#[cfg(feature = "backtrace")]
14use crate::proto::{trace, ExtBacktrace};
15
16pub type Result<T> = ::std::result::Result<T, Error>;
18
19#[derive(Debug, Error)]
21#[non_exhaustive]
22pub enum ErrorKind {
23 #[error("error recovering from journal: {}", _0)]
25 Recovery(&'static str),
26
27 #[error("wrong insert count: {} expect: {}", got, expect)]
29 WrongInsertCount {
30 got: usize,
32 expect: usize,
34 },
35
36 #[error("proto error: {0}")]
39 Proto(#[from] ProtoError),
40
41 #[cfg(feature = "sqlite")]
43 #[cfg_attr(docsrs, doc(cfg(feature = "sqlite")))]
44 #[error("sqlite error: {0}")]
45 Sqlite(#[from] rusqlite::Error),
46
47 #[error("request timed out")]
49 Timeout,
50}
51
52#[derive(Debug, Error)]
54pub struct Error {
55 kind: ErrorKind,
56 #[cfg(feature = "backtrace")]
57 backtrack: Option<ExtBacktrace>,
58}
59
60impl Error {
61 pub fn kind(&self) -> &ErrorKind {
63 &self.kind
64 }
65}
66
67impl fmt::Display for Error {
68 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
69 cfg_if::cfg_if! {
70 if #[cfg(feature = "backtrace")] {
71 if let Some(ref backtrace) = self.backtrack {
72 fmt::Display::fmt(&self.kind, f)?;
73 fmt::Debug::fmt(backtrace, f)
74 } else {
75 fmt::Display::fmt(&self.kind, f)
76 }
77 } else {
78 fmt::Display::fmt(&self.kind, f)
79 }
80 }
81 }
82}
83
84impl From<ErrorKind> for Error {
85 fn from(kind: ErrorKind) -> Self {
86 Self {
87 kind,
88 #[cfg(feature = "backtrace")]
89 backtrack: trace!(),
90 }
91 }
92}
93
94impl From<ProtoError> for Error {
95 fn from(e: ProtoError) -> Self {
96 match *e.kind() {
97 ProtoErrorKind::Timeout => ErrorKind::Timeout.into(),
98 _ => ErrorKind::from(e).into(),
99 }
100 }
101}
102
103#[cfg(feature = "sqlite")]
104#[cfg_attr(docsrs, doc(cfg(feature = "sqlite")))]
105impl From<rusqlite::Error> for Error {
106 fn from(e: rusqlite::Error) -> Self {
107 ErrorKind::from(e).into()
108 }
109}