rustbreak/
error.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5/// An error returned by a `DeSer` implementor
6#[derive(Debug, thiserror::Error)]
7#[non_exhaustive]
8#[allow(clippy::empty_enum)] // This can occur when no desers have beeen enabled
9pub enum DeSerError {
10    #[cfg(feature = "yaml_enc")]
11    /// An error occured with Yaml
12    #[error("An error with yaml occured")]
13    Yaml(#[from] serde_yaml::Error),
14    #[cfg(feature = "ron_enc")]
15    /// An error occured with Ron
16    #[error("An error with Ron occured")]
17    Ron(#[from] ron::Error),
18    #[cfg(feature = "bin_enc")]
19    /// An error occured with Bincode
20    #[error("An error with Bincode occured")]
21    Bincode(#[from] std::boxed::Box<bincode::ErrorKind>),
22    /// An internal error to Rustbreak occured
23    #[error("An internal error to rustbreak occured, please report it to the maintainers")]
24    Internal(String),
25    #[cfg(feature = "other_errors")]
26    /// A dynamic error occured
27    ///
28    /// Most likely the custom `DeSer` implementation has thrown an error, consult its documentation
29    /// for more information
30    ///
31    /// **Important**: This can only be used if the `other_errors` feature is enabled
32    #[error(transparent)]
33    Other(#[from] anyhow::Error),
34}
35
36/// An error returned by a Backend implementor
37#[derive(Debug, thiserror::Error)]
38#[non_exhaustive]
39pub enum BackendError {
40    /// An error occured from the tempfile
41    #[error("An error while persisting the file occured")]
42    TempFile(#[from] tempfile::PersistError),
43    /// An I/O Error occured
44    #[error("An I/O Error occured")]
45    Io(#[from] std::io::Error),
46    /// An internal error to Rustbreak occured
47    #[error("An internal error to rustbreak occured, please report it to the maintainers")]
48    Internal(String),
49    #[cfg(feature = "other_errors")]
50    /// A dynamic error occured
51    ///
52    /// Most likely the custom `Backend` implementation has thrown an error, consult its documentation
53    /// for more information
54    ///
55    /// **Important**: This can only be used if the `other_errors` feature is enabled
56    #[error(transparent)]
57    Other(#[from] anyhow::Error),
58}
59
60/// The different kinds of errors that can be returned
61#[derive(Debug, thiserror::Error)]
62#[non_exhaustive]
63pub enum RustbreakError {
64    /// A context error when a DeSerialization failed
65    #[error("Could not deserialize the value")]
66    DeSerialization(#[from] DeSerError),
67    /// This error is returned if the `Database` is poisoned. See
68    /// `Database::write` for details
69    #[error("The database has been poisoned")]
70    Poison,
71    /// An error in the backend happened
72    #[error("The backend has encountered an error")]
73    Backend(#[from] BackendError),
74    /// If `Database::write_safe` is used and the closure panics, this error is
75    /// returned
76    #[error("The write operation paniced but got caught")]
77    WritePanic,
78}
79
80/// A simple type alias for errors
81pub type Result<T> = std::result::Result<T, RustbreakError>;
82/// The type alias used for backends
83pub type BackendResult<T> = std::result::Result<T, BackendError>;
84/// The type alias used for `DeSer`s
85pub type DeSerResult<T> = std::result::Result<T, DeSerError>;