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