Skip to main content

systemprompt_analytics/
error.rs

1//! Typed error boundary for the `systemprompt-analytics` crate.
2
3use systemprompt_models::domain_error;
4
5domain_error! {
6    pub enum AnalyticsError {
7        common: [repository, io, json, anyhow],
8
9        #[error("Session not found: {0}")]
10        SessionNotFound(String),
11
12        #[error("Invalid fingerprint hash: {0}")]
13        InvalidFingerprint(String),
14
15        #[error("Missing field: {0}")]
16        MissingField(String),
17
18        #[error("Invalid argument: {0}")]
19        InvalidArgument(String),
20
21        #[error("Session expired")]
22        SessionExpired,
23
24        #[error("Throttle level exceeded")]
25        ThrottleLevelExceeded,
26
27        #[error("Behavioral bot detected: {0}")]
28        BehavioralBotDetected(String),
29
30        #[error("Anomaly detection failed: {0}")]
31        AnomalyDetectionFailed(String),
32    }
33}
34
35impl From<sqlx::Error> for AnalyticsError {
36    fn from(err: sqlx::Error) -> Self {
37        Self::Repository(systemprompt_database::RepositoryError::from(err))
38    }
39}
40
41impl AnalyticsError {
42    pub fn missing_field<T: std::fmt::Display>(field: T) -> Self {
43        Self::MissingField(field.to_string())
44    }
45
46    pub fn invalid_argument<T: Into<String>>(message: T) -> Self {
47        Self::InvalidArgument(message.into())
48    }
49}
50
51pub type Result<T> = std::result::Result<T, AnalyticsError>;