Skip to main content

systemprompt_logging/models/
log_error.rs

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