Skip to main content

systemprompt_agent/services/shared/
error.rs

1//! Service-layer error type, distinct from the crate-public [`AgentError`]:
2//! it models failures internal to runtime services and converts into
3//! `AgentError` at the crate boundary.
4//!
5//! [`AgentError`]: crate::error::AgentError
6
7use thiserror::Error;
8
9#[derive(Debug, Error)]
10pub enum AgentServiceError {
11    #[error("database operation failed: {0}")]
12    Database(String),
13
14    #[error("repository operation failed: {0}")]
15    Repository(String),
16
17    #[error("network request failed: {0}")]
18    Network(String),
19
20    #[error("authentication failed: {0}")]
21    Authentication(String),
22
23    #[error("authorization failed for resource: {0}")]
24    Authorization(String),
25
26    #[error("validation failed: {0}: {1}")]
27    Validation(String, String),
28
29    #[error("resource not found: {0}")]
30    NotFound(String),
31
32    #[error("service unavailable: {0}")]
33    ServiceUnavailable(String),
34
35    #[error("operation timed out after {0}ms")]
36    Timeout(u64),
37
38    #[error("configuration error: {0}: {1}")]
39    Configuration(String, String),
40
41    #[error("conflict: {0}")]
42    Conflict(String),
43
44    #[error("internal error: {0}")]
45    Internal(String),
46
47    #[error("logging error: {0}")]
48    Logging(String),
49
50    #[error("capacity exceeded: {0}")]
51    Capacity(String),
52}
53
54impl From<std::io::Error> for AgentServiceError {
55    fn from(err: std::io::Error) -> Self {
56        Self::Internal(format!("io: {err}"))
57    }
58}
59
60impl From<sqlx::Error> for AgentServiceError {
61    fn from(err: sqlx::Error) -> Self {
62        Self::Database(err.to_string())
63    }
64}
65
66impl From<crate::repository::RepositoryError> for AgentServiceError {
67    fn from(err: crate::repository::RepositoryError) -> Self {
68        Self::Repository(err.to_string())
69    }
70}
71
72impl From<systemprompt_database::RepositoryError> for AgentServiceError {
73    fn from(err: systemprompt_database::RepositoryError) -> Self {
74        Self::Repository(err.to_string())
75    }
76}
77
78impl From<crate::error::AgentError> for AgentServiceError {
79    fn from(err: crate::error::AgentError) -> Self {
80        Self::Internal(err.to_string())
81    }
82}
83
84impl From<reqwest::Error> for AgentServiceError {
85    fn from(err: reqwest::Error) -> Self {
86        Self::Network(
87            err.url()
88                .map_or_else(|| "unknown".to_owned(), ToString::to_string),
89        )
90    }
91}
92
93pub type Result<T> = std::result::Result<T, AgentServiceError>;