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 owner: {0}")]
13        SessionOwner(#[from] systemprompt_traits::AnalyticsProviderError),
14
15        #[error("Analytics event store failed: {0}")]
16        EventStore(#[from] systemprompt_traits::RepositoryError),
17
18        #[error("Session not found: {0}")]
19        SessionNotFound(String),
20
21        #[error("Invalid fingerprint hash: {0}")]
22        InvalidFingerprint(String),
23
24        #[error("Missing field: {0}")]
25        MissingField(String),
26
27        #[error("Invalid argument: {0}")]
28        InvalidArgument(String),
29
30        #[error("Reporting baseline rebuild superseded by a newer generation")]
31        RebuildSuperseded,
32
33        #[error("Session expired")]
34        SessionExpired,
35
36        #[error("Behavioral bot detected: {0}")]
37        BehavioralBotDetected(String),
38
39        #[error("Anomaly detection failed: {0}")]
40        AnomalyDetectionFailed(String),
41    }
42}
43
44impl From<sqlx::Error> for AnalyticsError {
45    fn from(err: sqlx::Error) -> Self {
46        Self::Repository(systemprompt_database::RepositoryError::from(err))
47    }
48}
49
50impl AnalyticsError {
51    pub fn missing_field<T: std::fmt::Display>(field: T) -> Self {
52        Self::MissingField(field.to_string())
53    }
54
55    pub fn invalid_argument<T: Into<String>>(message: T) -> Self {
56        Self::InvalidArgument(message.into())
57    }
58
59    pub const fn rebuild_superseded() -> Self {
60        Self::RebuildSuperseded
61    }
62
63    pub const fn is_rebuild_superseded(&self) -> bool {
64        matches!(self, Self::RebuildSuperseded)
65    }
66}
67
68pub type Result<T> = std::result::Result<T, AnalyticsError>;