Skip to main content

thingd_core/
error.rs

1//! Error types for thingd core operations.
2
3use std::error::Error;
4use std::fmt::{Display, Formatter, Result as FormatResult};
5
6/// Result type returned by thingd core operations.
7pub type ThingdResult<T> = Result<T, ThingdError>;
8
9/// Error type returned by thingd core operations.
10#[derive(Clone, Debug, Eq, PartialEq)]
11pub enum ThingdError {
12    /// The caller provided invalid input.
13    InvalidInput(String),
14    /// A requested record could not be found.
15    NotFound(String),
16    /// The requested operation conflicts with current state.
17    Conflict(String),
18    /// The storage adapter failed.
19    Storage(String),
20}
21
22impl Display for ThingdError {
23    fn fmt(&self, formatter: &mut Formatter<'_>) -> FormatResult {
24        match self {
25            Self::InvalidInput(message) => write!(formatter, "invalid input: {message}"),
26            Self::NotFound(message) => write!(formatter, "not found: {message}"),
27            Self::Conflict(message) => write!(formatter, "conflict: {message}"),
28            Self::Storage(message) => write!(formatter, "storage error: {message}"),
29        }
30    }
31}
32
33impl Error for ThingdError {}
34
35#[cfg(feature = "sqlite")]
36impl From<rusqlite::Error> for ThingdError {
37    fn from(error: rusqlite::Error) -> Self {
38        Self::Storage(error.to_string())
39    }
40}