Skip to main content

ferogram_fsm/
error.rs

1/*
2 * Copyright (c) 2026 Ankit Chaubey <ankitchaubey.dev@gmail.com>
3 * https://github.com/ankit-chaubey
4 *
5 * Project: ferogram
6 * Website: https://ferogram.dev
7 *
8 * Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
9 * https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
10 * <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
11 * This file may not be copied, modified, or distributed except according
12 * to those terms.
13 */
14
15use std::fmt;
16
17/// An error from a [`StateStorage`](crate::StateStorage) backend.
18#[derive(Debug)]
19pub struct StorageError {
20    message: String,
21    source: Option<Box<dyn std::error::Error + Send + Sync>>,
22}
23
24impl StorageError {
25    /// Create a storage error with a plain message.
26    pub fn new(message: impl Into<String>) -> Self {
27        Self {
28            message: message.into(),
29            source: None,
30        }
31    }
32
33    /// Create a storage error wrapping an underlying cause.
34    pub fn with_source(
35        message: impl Into<String>,
36        source: impl std::error::Error + Send + Sync + 'static,
37    ) -> Self {
38        Self {
39            message: message.into(),
40            source: Some(Box::new(source)),
41        }
42    }
43}
44
45impl fmt::Display for StorageError {
46    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47        write!(f, "state storage error: {}", self.message)?;
48        if let Some(ref src) = self.source {
49            write!(f, ": {src}")?;
50        }
51        Ok(())
52    }
53}
54
55impl std::error::Error for StorageError {
56    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
57        self.source.as_ref().map(|e| e.as_ref() as _)
58    }
59}