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    #[error("skill {skill_id} is managed but withheld ({reason})")]
57    SkillWithheld {
58        skill_id: systemprompt_identifiers::SkillId,
59        reason: &'static str,
60    },
61
62    #[error("skill {skill_id} could not be resolved: {source}")]
63    SkillSource {
64        skill_id: systemprompt_identifiers::SkillId,
65        #[source]
66        source: systemprompt_traits::ManagedSkillResolverError,
67    },
68}
69
70impl From<std::io::Error> for AgentServiceError {
71    fn from(err: std::io::Error) -> Self {
72        Self::Internal(format!("io: {err}"))
73    }
74}
75
76impl From<sqlx::Error> for AgentServiceError {
77    fn from(err: sqlx::Error) -> Self {
78        Self::Database(err.to_string())
79    }
80}
81
82impl From<crate::repository::RepositoryError> for AgentServiceError {
83    fn from(err: crate::repository::RepositoryError) -> Self {
84        Self::Repository(err.to_string())
85    }
86}
87
88impl From<systemprompt_database::RepositoryError> for AgentServiceError {
89    fn from(err: systemprompt_database::RepositoryError) -> Self {
90        Self::Repository(err.to_string())
91    }
92}
93
94impl From<crate::error::AgentError> for AgentServiceError {
95    fn from(err: crate::error::AgentError) -> Self {
96        Self::Internal(err.to_string())
97    }
98}
99
100impl From<systemprompt_models::errors::ProviderError> for AgentServiceError {
101    fn from(err: systemprompt_models::errors::ProviderError) -> Self {
102        Self::Internal(err.to_string())
103    }
104}
105
106impl From<reqwest::Error> for AgentServiceError {
107    fn from(err: reqwest::Error) -> Self {
108        Self::Network(
109            err.url()
110                .map_or_else(|| "unknown".to_owned(), ToString::to_string),
111        )
112    }
113}
114
115pub type Result<T> = std::result::Result<T, AgentServiceError>;