|
| 1 | +use serde::{Deserialize, Serialize}; |
| 2 | +use thiserror::Error; |
| 3 | + |
| 4 | +/// Common error type for all state query responses |
| 5 | +#[derive(Debug, Clone, Error, Serialize, Deserialize)] |
| 6 | +pub enum QueryError { |
| 7 | + /// The requested resource was not found |
| 8 | + #[error("Not found: {resource}")] |
| 9 | + NotFound { resource: String }, |
| 10 | + |
| 11 | + /// An error occurred while processing the query |
| 12 | + #[error("Internal error: {message}")] |
| 13 | + Internal { message: String }, |
| 14 | + |
| 15 | + /// Storage backend is disabled in configuration |
| 16 | + #[error("{storage_type} storage is not enabled")] |
| 17 | + StorageDisabled { storage_type: String }, |
| 18 | + |
| 19 | + /// Invalid request parameters |
| 20 | + #[error("Invalid request: {message}")] |
| 21 | + InvalidRequest { message: String }, |
| 22 | + |
| 23 | + /// Query variant is not implemented yet |
| 24 | + #[error("Query not implemented: {query}")] |
| 25 | + NotImplemented { query: String }, |
| 26 | +} |
| 27 | + |
| 28 | +impl QueryError { |
| 29 | + pub fn not_found(resource: impl Into<String>) -> Self { |
| 30 | + Self::NotFound { |
| 31 | + resource: resource.into(), |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + pub fn internal_error(message: impl Into<String>) -> Self { |
| 36 | + Self::Internal { |
| 37 | + message: message.into(), |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + pub fn storage_disabled(storage_type: impl Into<String>) -> Self { |
| 42 | + Self::StorageDisabled { |
| 43 | + storage_type: storage_type.into(), |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + pub fn invalid_request(message: impl Into<String>) -> Self { |
| 48 | + Self::InvalidRequest { |
| 49 | + message: message.into(), |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + pub fn not_implemented(query: impl Into<String>) -> Self { |
| 54 | + Self::NotImplemented { |
| 55 | + query: query.into(), |
| 56 | + } |
| 57 | + } |
| 58 | +} |
0 commit comments