Skip to main content

wp_connector_api/errors/
source.rs

1use derive_more::From;
2use orion_error::conversion::ToStructError;
3use orion_error::{OrionError, StructError, UvsReason};
4use serde::Serialize;
5use std::error::Error as StdError;
6
7#[derive(Debug, Clone, PartialEq, Serialize, From, OrionError)]
8pub enum SourceReason {
9    #[orion_error(identity = "biz.not_data", message = "not data")]
10    NotData,
11    #[orion_error(identity = "biz.eof", message = "eof")]
12    EOF,
13    #[orion_error(identity = "biz.supplier_error")]
14    SupplierError(String),
15    #[orion_error(identity = "biz.disconnect")]
16    #[from(skip)]
17    Disconnect(String),
18    #[orion_error(identity = "biz.other")]
19    #[from(skip)]
20    Other(String),
21    #[orion_error(transparent)]
22    Uvs(UvsReason),
23}
24
25pub type SourceError = StructError<SourceReason>;
26pub type SourceResult<T> = Result<T, StructError<SourceReason>>;
27
28impl SourceReason {
29    pub fn err(self) -> SourceError {
30        self.to_err()
31    }
32
33    pub fn err_detail<S: Into<String>>(self, detail: S) -> SourceError {
34        self.to_err().with_detail(detail.into())
35    }
36
37    pub fn err_source<E>(self, source: E) -> SourceError
38    where
39        E: StdError + Send + Sync + 'static,
40    {
41        self.to_err().with_source(source)
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn source_reason_err_detail_sets_detail() {
51        let err = SourceReason::Other("boom".into()).err_detail("ctx");
52        assert_eq!(err.detail().as_deref(), Some("ctx"));
53    }
54
55    #[test]
56    fn source_reason_err_source_preserves_source_message() {
57        let err = SourceReason::Disconnect("read failed".into())
58            .err_source(std::io::Error::other("disk gone"));
59        assert!(err.to_string().contains("disk gone"));
60    }
61}