trust_dns_server/error/
persistence_error.rs

1// Copyright 2015-2020 Benjamin Fry <benjaminfry@me.com>
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8use std::fmt;
9
10use crate::proto::error::*;
11use thiserror::Error;
12
13#[cfg(feature = "backtrace")]
14use crate::proto::{trace, ExtBacktrace};
15
16/// An alias for results returned by functions of this crate
17pub type Result<T> = ::std::result::Result<T, Error>;
18
19/// The error kind for errors that get returned in the crate
20#[derive(Debug, Error)]
21#[non_exhaustive]
22pub enum ErrorKind {
23    /// An error that occurred when recovering from journal
24    #[error("error recovering from journal: {}", _0)]
25    Recovery(&'static str),
26
27    /// The number of inserted records didn't match the expected amount
28    #[error("wrong insert count: {} expect: {}", got, expect)]
29    WrongInsertCount {
30        /// The number of inserted records
31        got: usize,
32        /// The number of records expected to be inserted
33        expect: usize,
34    },
35
36    // foreign
37    /// An error got returned by the trust-dns-proto crate
38    #[error("proto error: {0}")]
39    Proto(#[from] ProtoError),
40
41    /// An error got returned from the sqlite crate
42    #[cfg(feature = "sqlite")]
43    #[cfg_attr(docsrs, doc(cfg(feature = "sqlite")))]
44    #[error("sqlite error: {0}")]
45    Sqlite(#[from] rusqlite::Error),
46
47    /// A request timed out
48    #[error("request timed out")]
49    Timeout,
50}
51
52/// The error type for errors that get returned in the crate
53#[derive(Debug, Error)]
54pub struct Error {
55    kind: ErrorKind,
56    #[cfg(feature = "backtrace")]
57    backtrack: Option<ExtBacktrace>,
58}
59
60impl Error {
61    /// Get the kind of the error
62    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}