Skip to main content

systemprompt_database/
error.rs

1use thiserror::Error;
2
3#[derive(Debug, Error)]
4pub enum RepositoryError {
5    #[error("Entity not found: {0}")]
6    NotFound(String),
7
8    #[error("Constraint violation: {0}")]
9    Constraint(String),
10
11    #[error("Database error: {0}")]
12    Database(#[from] sqlx::Error),
13
14    #[error("Serialization error: {0}")]
15    Serialization(#[from] serde_json::Error),
16
17    #[error("Invalid argument: {0}")]
18    InvalidArgument(String),
19
20    #[error("Internal error: {0}")]
21    Internal(String),
22}
23
24impl RepositoryError {
25    pub fn not_found<T: std::fmt::Display>(id: T) -> Self {
26        Self::NotFound(id.to_string())
27    }
28
29    pub fn constraint<T: Into<String>>(message: T) -> Self {
30        Self::Constraint(message.into())
31    }
32
33    pub fn invalid_argument<T: Into<String>>(message: T) -> Self {
34        Self::InvalidArgument(message.into())
35    }
36
37    pub fn internal<T: Into<String>>(message: T) -> Self {
38        Self::Internal(message.into())
39    }
40
41    #[must_use]
42    pub const fn is_not_found(&self) -> bool {
43        matches!(self, Self::NotFound(_))
44    }
45
46    #[must_use]
47    pub const fn is_constraint(&self) -> bool {
48        matches!(self, Self::Constraint(_))
49    }
50}
51
52impl From<anyhow::Error> for RepositoryError {
53    fn from(err: anyhow::Error) -> Self {
54        Self::Internal(err.to_string())
55    }
56}