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//!
7//! Copyright (c) systemprompt.io — Business Source License 1.1.
8//! See <https://systemprompt.io> for licensing details.
9
10use thiserror::Error;
11
12#[derive(Debug, Error)]
13pub enum AgentServiceError {
14    #[error("database operation failed: {0}")]
15    Database(String),
16
17    #[error("repository operation failed: {0}")]
18    Repository(String),
19
20    #[error("network request failed: {0}")]
21    Network(String),
22
23    #[error("authentication failed: {0}")]
24    Authentication(String),
25
26    #[error("authorization failed for resource: {0}")]
27    Authorization(String),
28
29    #[error("validation failed: {0}: {1}")]
30    Validation(String, String),
31
32    #[error("resource not found: {0}")]
33    NotFound(String),
34
35    #[error("service unavailable: {0}")]
36    ServiceUnavailable(String),
37
38    #[error("operation timed out after {0}ms")]
39    Timeout(u64),
40
41    #[error("configuration error: {0}: {1}")]
42    Configuration(String, String),
43
44    #[error("conflict: {0}")]
45    Conflict(String),
46
47    #[error("internal error: {0}")]
48    Internal(String),
49
50    #[error("logging error: {0}")]
51    Logging(String),
52
53    #[error("capacity exceeded: {0}")]
54    Capacity(String),
55}
56
57impl From<std::io::Error> for AgentServiceError {
58    fn from(err: std::io::Error) -> Self {
59        Self::Internal(format!("io: {err}"))
60    }
61}
62
63impl From<sqlx::Error> for AgentServiceError {
64    fn from(err: sqlx::Error) -> Self {
65        Self::Database(err.to_string())
66    }
67}
68
69impl From<crate::repository::RepositoryError> for AgentServiceError {
70    fn from(err: crate::repository::RepositoryError) -> Self {
71        Self::Repository(err.to_string())
72    }
73}
74
75impl From<systemprompt_database::RepositoryError> for AgentServiceError {
76    fn from(err: systemprompt_database::RepositoryError) -> Self {
77        Self::Repository(err.to_string())
78    }
79}
80
81impl From<crate::error::AgentError> for AgentServiceError {
82    fn from(err: crate::error::AgentError) -> Self {
83        Self::Internal(err.to_string())
84    }
85}
86
87impl From<systemprompt_models::errors::ProviderError> for AgentServiceError {
88    fn from(err: systemprompt_models::errors::ProviderError) -> Self {
89        Self::Internal(err.to_string())
90    }
91}
92
93impl From<reqwest::Error> for AgentServiceError {
94    fn from(err: reqwest::Error) -> Self {
95        Self::Network(
96            err.url()
97                .map_or_else(|| "unknown".to_owned(), ToString::to_string),
98        )
99    }
100}
101
102pub type Result<T> = std::result::Result<T, AgentServiceError>;