joydb/
error.rs

1use std::path::PathBuf;
2use thiserror::Error;
3
4/// Joydb error type.
5#[derive(Debug, Error)]
6pub enum JoydbError {
7    /// File system related error.
8    #[error("IO error: {0}")]
9    Io(#[from] std::io::Error),
10
11    /// A path expected to be a file, but it is not.
12    #[error("{0} is not a file")]
13    NotFile(PathBuf),
14
15    /// A path expected to be a directory, but it is not.
16    #[error("{0} is not a directory")]
17    NotDirectory(PathBuf),
18
19    /// Serialization error.
20    /// This may occur when adapter format is not supporting the data type.
21    /// For example, if you try to serialize a HashMap with K type as a complex structure to JSON.
22    #[error("Serialize error: {0}")]
23    Serialize(Box<dyn std::error::Error + Send + Sync>),
24
25    /// Deserialization error.
26    /// May occur on opening a file.
27    #[error("Deserialize error: {0}")]
28    Deserialize(Box<dyn std::error::Error + Send + Sync>),
29
30    /// Error when trying to insert a model with an ID that already exists.
31    #[error("{model} with id = {id} already exists")]
32    DuplicatedId {
33        /// ID of the model formatted with `Debug`
34        id: String,
35        /// Name of the model (type name)
36        model: String,
37    },
38
39    /// Error when trying to update a model with an ID that does not exist.
40    #[error("{model} with id = {id} not found")]
41    NotFound {
42        /// ID of the model formatted with `Debug`
43        id: String,
44        /// Name of the model (type name)
45        model: String,
46    },
47
48    /// Custom error variant. Intended for third party adapters for situations
49    /// when non of the existing variants are suitable.
50    #[error("Custom error: {0}")]
51    Custom(Box<dyn std::error::Error + Send + Sync>),
52}