spectra_core/error.rs
1use thiserror::Error;
2
3/// Result type returned by Spectra operations.
4///
5/// # Examples
6///
7/// ```
8/// use spectra_core::Result;
9///
10/// fn validate_name(name: &str) -> Result<()> {
11/// if name.is_empty() {
12/// return Err(spectra_core::Error::Internal("metric name is empty".into()));
13/// }
14/// Ok(())
15/// }
16///
17/// assert!(validate_name("cache_hits").is_ok());
18/// ```
19pub type Result<T> = std::result::Result<T, Error>;
20
21/// Errors returned by Spectra storage, routing, serialization, and I/O paths.
22///
23/// Backend-specific failures that do not map to I/O or JSON are reported as
24/// [`Internal`](Self::Internal) with their original message.
25///
26/// # Examples
27///
28/// ```
29/// use spectra_core::Error;
30///
31/// let error = Error::Internal("metrics backend is required".into());
32/// assert_eq!(error.to_string(), "metrics backend is required");
33///
34/// match error {
35/// Error::Internal(message) => assert!(message.contains("backend")),
36/// Error::Io(_) | Error::Json(_) => unreachable!(),
37/// }
38/// ```
39#[derive(Debug, Error)]
40pub enum Error {
41 /// Underlying filesystem or stream I/O failure.
42 #[error("io error: {0}")]
43 Io(#[from] std::io::Error),
44 /// JSON serialization or deserialization failure.
45 #[error("json error: {0}")]
46 Json(#[from] serde_json::Error),
47 /// Catch-all internal error with a message.
48 #[error("{0}")]
49 Internal(String),
50}