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],
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("Behavioral bot detected: {0}")]
25        BehavioralBotDetected(String),
26
27        #[error("Anomaly detection failed: {0}")]
28        AnomalyDetectionFailed(String),
29    }
30}
31
32impl From<sqlx::Error> for AnalyticsError {
33    fn from(err: sqlx::Error) -> Self {
34        Self::Repository(systemprompt_database::RepositoryError::from(err))
35    }
36}
37
38impl AnalyticsError {
39    pub fn missing_field<T: std::fmt::Display>(field: T) -> Self {
40        Self::MissingField(field.to_string())
41    }
42
43    pub fn invalid_argument<T: Into<String>>(message: T) -> Self {
44        Self::InvalidArgument(message.into())
45    }
46}
47
48pub type Result<T> = std::result::Result<T, AnalyticsError>;