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("Repository operation failed: {0}")]
30    Repository(#[from] systemprompt_traits::RepositoryError),
31
32    #[error("JSON serialization failed")]
33    JsonError(#[from] serde_json::Error),
34
35    #[error("UUID generation failed")]
36    UuidError(#[from] uuid::Error),
37
38    #[error("DateTime parsing failed")]
39    DateTimeError(#[from] chrono::ParseError),
40
41    #[error("Cleanup operation failed: deleted {count} entries")]
42    CleanupError { count: u64 },
43
44    #[error("Pagination parameters invalid: page={page}, per_page={per_page}")]
45    PaginationError { page: i32, per_page: i32 },
46
47    #[error("Log filter invalid: {filter_type}={value}")]
48    FilterError { filter_type: String, value: String },
49
50    #[error("Terminal output failed")]
51    TerminalError,
52
53    #[error("Database connection not available")]
54    DatabaseUnavailable,
55
56    #[error("No task found matching: {partial_id}")]
57    TaskNotFound { partial_id: String },
58}
59
60impl LoggingError {
61    pub fn invalid_log_entry(field: impl Into<String>, reason: impl Into<String>) -> Self {
62        Self::InvalidLogEntry {
63            field: field.into(),
64            reason: reason.into(),
65        }
66    }
67
68    pub fn validation_error(message: impl Into<String>) -> Self {
69        Self::ValidationError {
70            message: message.into(),
71        }
72    }
73
74    pub fn invalid_log_level(level: impl Into<String>) -> Self {
75        Self::InvalidLogLevel {
76            level: level.into(),
77        }
78    }
79
80    pub fn log_entry_not_found(id: impl Into<String>) -> Self {
81        Self::LogEntryNotFound { id: id.into() }
82    }
83
84    pub const fn cleanup_error(count: u64) -> Self {
85        Self::CleanupError { count }
86    }
87
88    pub const fn pagination_error(page: i32, per_page: i32) -> Self {
89        Self::PaginationError { page, per_page }
90    }
91
92    pub fn filter_error(filter_type: impl Into<String>, value: impl Into<String>) -> Self {
93        Self::FilterError {
94            filter_type: filter_type.into(),
95            value: value.into(),
96        }
97    }
98}
99
100impl From<sqlx::Error> for LoggingError {
101    fn from(err: sqlx::Error) -> Self {
102        Self::Repository(systemprompt_traits::RepositoryError::from(err))
103    }
104}