Skip to main content

systemprompt_logging/models/
log_error.rs

1//! Logging error types.
2//!
3//! Copyright (c) systemprompt.io — Business Source License 1.1.
4//! See <https://systemprompt.io> for licensing details.
5
6#[derive(Debug, thiserror::Error)]
7pub enum LoggingError {
8    #[error("Invalid log entry: {field} {reason}")]
9    InvalidLogEntry { field: String, reason: String },
10
11    #[error("Log entry validation failed: {message}")]
12    ValidationError { message: String },
13
14    #[error("Invalid log level: {level}")]
15    InvalidLogLevel { level: String },
16
17    #[error("Log entry not found: {id}")]
18    LogEntryNotFound { id: String },
19
20    #[error("Empty log module name")]
21    EmptyModuleName,
22
23    #[error("Empty log message")]
24    EmptyMessage,
25
26    #[error("Invalid metadata format")]
27    InvalidMetadata,
28
29    #[error("Database operation failed")]
30    DatabaseError(#[from] sqlx::Error),
31
32    #[error("Repository operation failed: {0}")]
33    Repository(#[from] systemprompt_database::RepositoryError),
34
35    #[error("JSON serialization failed")]
36    JsonError(#[from] serde_json::Error),
37
38    #[error("UUID generation failed")]
39    UuidError(#[from] uuid::Error),
40
41    #[error("DateTime parsing failed")]
42    DateTimeError(#[from] chrono::ParseError),
43
44    #[error("Log repository operation failed: {operation}")]
45    RepositoryError { operation: String },
46
47    #[error("Cleanup operation failed: deleted {count} entries")]
48    CleanupError { count: u64 },
49
50    #[error("Pagination parameters invalid: page={page}, per_page={per_page}")]
51    PaginationError { page: i32, per_page: i32 },
52
53    #[error("Log filter invalid: {filter_type}={value}")]
54    FilterError { filter_type: String, value: String },
55
56    #[error("Terminal output failed")]
57    TerminalError,
58
59    #[error("Database connection not available")]
60    DatabaseUnavailable,
61
62    #[error("Database pool unavailable: {0}")]
63    PoolUnavailable(String),
64
65    #[error("Retention scheduler error")]
66    Scheduler(#[from] tokio_cron_scheduler::JobSchedulerError),
67
68    #[error("No task found matching: {partial_id}")]
69    TaskNotFound { partial_id: String },
70}
71
72impl LoggingError {
73    pub fn invalid_log_entry(field: impl Into<String>, reason: impl Into<String>) -> Self {
74        Self::InvalidLogEntry {
75            field: field.into(),
76            reason: reason.into(),
77        }
78    }
79
80    pub fn validation_error(message: impl Into<String>) -> Self {
81        Self::ValidationError {
82            message: message.into(),
83        }
84    }
85
86    pub fn invalid_log_level(level: impl Into<String>) -> Self {
87        Self::InvalidLogLevel {
88            level: level.into(),
89        }
90    }
91
92    pub fn log_entry_not_found(id: impl Into<String>) -> Self {
93        Self::LogEntryNotFound { id: id.into() }
94    }
95
96    pub fn repository_error(operation: impl Into<String>) -> Self {
97        Self::RepositoryError {
98            operation: operation.into(),
99        }
100    }
101
102    pub const fn cleanup_error(count: u64) -> Self {
103        Self::CleanupError { count }
104    }
105
106    pub const fn pagination_error(page: i32, per_page: i32) -> Self {
107        Self::PaginationError { page, per_page }
108    }
109
110    pub fn filter_error(filter_type: impl Into<String>, value: impl Into<String>) -> Self {
111        Self::FilterError {
112            filter_type: filter_type.into(),
113            value: value.into(),
114        }
115    }
116
117    pub fn into_sqlx_error(self) -> sqlx::Error {
118        sqlx::Error::Protocol(format!("{self}"))
119    }
120}