Skip to main content

systemprompt_analytics/
error.rs

1//! Typed error boundary for the `systemprompt-analytics` crate.
2//!
3//! Copyright (c) systemprompt.io — Business Source License 1.1.
4//! See <https://systemprompt.io> for licensing details.
5
6use systemprompt_models::domain_error;
7
8domain_error! {
9    pub enum AnalyticsError {
10        common: [repository, io, json],
11
12        #[error("Session not found: {0}")]
13        SessionNotFound(String),
14
15        #[error("Invalid fingerprint hash: {0}")]
16        InvalidFingerprint(String),
17
18        #[error("Missing field: {0}")]
19        MissingField(String),
20
21        #[error("Invalid argument: {0}")]
22        InvalidArgument(String),
23
24        #[error("Session expired")]
25        SessionExpired,
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>;