Skip to main content

feldera_adapterlib/errors/
journal.rs

1use crate::transport::Step;
2use dbsp::storage::backend::{StorageError, StoragePath};
3use serde::{Serialize, Serializer};
4use std::backtrace::Backtrace;
5use std::fmt::{Display, Formatter, Result as FmtResult};
6use std::io::ErrorKind;
7
8pub use crate::errors::controller::ControllerError;
9
10#[derive(Debug, Serialize)]
11#[serde(untagged)]
12#[doc(hidden)]
13pub enum StepError {
14    /// Storage error.
15    StorageError {
16        path: String,
17        error: StorageError,
18        #[serde(serialize_with = "serialize_as_string")]
19        backtrace: Backtrace,
20    },
21
22    EncodeError {
23        path: String,
24        #[serde(serialize_with = "serialize_as_string")]
25        error: rmp_serde::encode::Error,
26    },
27
28    DecodeError {
29        path: String,
30        #[serde(serialize_with = "serialize_as_string")]
31        error: rmp_serde::decode::Error,
32    },
33
34    WrongStep {
35        path: String,
36        expected: Step,
37        found: Step,
38    },
39}
40
41impl StepError {
42    #[doc(hidden)]
43    pub fn kind(&self) -> ErrorKind {
44        match self {
45            Self::StorageError { error, .. } => error.kind(),
46            Self::EncodeError { .. } | Self::DecodeError { .. } | Self::WrongStep { .. } => {
47                ErrorKind::Other
48            }
49        }
50    }
51}
52
53fn serialize_as_string<S, T>(value: &T, serializer: S) -> Result<S::Ok, S::Error>
54where
55    S: Serializer,
56    T: ToString,
57{
58    serializer.serialize_str(&value.to_string())
59}
60
61impl Display for StepError {
62    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
63        match self {
64            StepError::EncodeError { path, error } => {
65                write!(f, "{path}: error writing step ({error})")
66            }
67            StepError::DecodeError { path, error } => {
68                write!(f, "{path}: error parsing step ({error})")
69            }
70            StepError::StorageError { path, error, .. } => write!(f, "{path}: {error}"),
71            StepError::WrongStep {
72                path,
73                expected,
74                found,
75            } => write!(
76                f,
77                "{path}: file should contain  step {expected}, but read step {found}"
78            ),
79        }
80    }
81}
82
83impl StepError {
84    #[doc(hidden)]
85    pub fn storage_error(path: &StoragePath, error: StorageError) -> StepError {
86        StepError::StorageError {
87            path: path.as_ref().into(),
88            error,
89            backtrace: Backtrace::capture(),
90        }
91    }
92}
93
94impl From<StepError> for ControllerError {
95    fn from(value: StepError) -> Self {
96        ControllerError::StepError(value)
97    }
98}