Skip to main content

wp_error/
run_error.rs

1use crate::parse_error::OMLCodeReason;
2use crate::{ConfReason, config_error::ConfCore};
3use derive_more::From;
4use orion_error::reason::ConfErrReason;
5use orion_error::{OrionError, StructError, UvsFrom, UvsReason};
6use orion_sec::OrionSecReason;
7use serde::Serialize;
8use thiserror::Error;
9
10pub type RunError = StructError<RunReason>;
11pub type RunResult<T> = Result<T, RunError>;
12
13#[derive(Debug, Error, PartialEq, Serialize, From)]
14pub enum DistFocus {
15    #[error("sink error : {0}")]
16    SinkError(String),
17    #[error("stg-ctrl")]
18    StgCtrl,
19}
20#[derive(Debug, Error, PartialEq, Serialize)]
21pub enum SourceFocus {
22    #[error("no data")]
23    NoData,
24    #[error("eof")]
25    Eof,
26    #[error("supplier error : {0}")]
27    SupplierError(String),
28    #[error("other : {0}")]
29    Other(String),
30    #[from(skip)]
31    #[error("disconnect : {0}")]
32    Disconnect(String),
33}
34
35#[derive(Debug, PartialEq, Serialize, From, OrionError)]
36pub enum RunReason {
37    #[orion_error(identity = "biz.dist")]
38    Dist(DistFocus),
39    #[orion_error(identity = "biz.source")]
40    Source(SourceFocus),
41    #[orion_error(transparent)]
42    Uvs(UvsReason),
43}
44
45impl From<ConfReason<ConfCore>> for RunReason {
46    fn from(_: ConfReason<ConfCore>) -> Self {
47        Self::from_conf_reason(ConfErrReason::Core)
48    }
49}
50impl From<OMLCodeReason> for RunReason {
51    fn from(_: OMLCodeReason) -> Self {
52        Self::from_conf()
53    }
54}
55
56impl From<OrionSecReason> for RunReason {
57    fn from(value: OrionSecReason) -> Self {
58        match value {
59            OrionSecReason::Sec(_) => Self::from_conf(),
60            OrionSecReason::Uvs(uvs_reason) => Self::Uvs(uvs_reason),
61        }
62    }
63}
64
65pub trait RunErrorOwe<T> {
66    fn owe_sink(self) -> RunResult<T>;
67    fn owe_source(self) -> RunResult<T>;
68}
69
70impl<T, E> RunErrorOwe<T> for Result<T, E>
71where
72    E: std::fmt::Display,
73{
74    fn owe_sink(self) -> RunResult<T> {
75        match self {
76            Ok(v) => Ok(v),
77            Err(e) => Err(StructError::from(RunReason::Dist(DistFocus::SinkError(
78                "sink fail".into(),
79            )))
80            .with_detail(e.to_string())),
81        }
82    }
83    fn owe_source(self) -> RunResult<T> {
84        match self {
85            Ok(v) => Ok(v),
86            Err(e) => Err(
87                StructError::from(RunReason::Source(SourceFocus::SupplierError(
88                    "source fail".into(),
89                )))
90                .with_detail(e.to_string()),
91            ),
92        }
93    }
94}
95
96/*
97
98pub trait Option2RunResult<T> {
99    fn owe_logic(self, msg: &str) -> RunResult<T>;
100}
101
102impl<T> Option2RunResult<T> for Option<T> {
103    fn owe_logic(self, msg: &str) -> RunResult<T> {
104        self.ok_or(RunError::from(UvsReason::from_logic(msg.to_string())))
105    }
106}
107
108*/
109impl From<SourceReason> for RunReason {
110    fn from(e: SourceReason) -> Self {
111        match e {
112            SourceReason::NotData => Self::Source(SourceFocus::NoData),
113            SourceReason::EOF => Self::Source(SourceFocus::Eof),
114            SourceReason::SupplierError(info) => Self::Source(SourceFocus::SupplierError(info)),
115            SourceReason::Disconnect(info) => Self::Source(SourceFocus::Disconnect(info)),
116            SourceReason::Other(info) => Self::Source(SourceFocus::Other(info)),
117            SourceReason::Uvs(uvs) => Self::Uvs(uvs),
118        }
119        //Self::Domain(RunReason::Source(e))
120    }
121}
122
123impl From<SinkReason> for RunReason {
124    fn from(e: SinkReason) -> Self {
125        match e {
126            SinkReason::Sink(info) => Self::Dist(DistFocus::SinkError(info)),
127            // Map mock to stage control path for now (no panic in production paths)
128            SinkReason::Mock => Self::Dist(DistFocus::StgCtrl),
129            SinkReason::StgCtrl => Self::Dist(DistFocus::StgCtrl),
130            SinkReason::Uvs(uvs) => Self::Uvs(uvs),
131        }
132    }
133}
134use orion_conf::error::ConfIOReason;
135use wp_connector_api::{SinkReason, SourceReason};
136impl From<ConfIOReason> for RunReason {
137    fn from(value: ConfIOReason) -> Self {
138        match value {
139            ConfIOReason::Other(_) => RunReason::from_conf(),
140            ConfIOReason::Uvs(uvs) => RunReason::Uvs(uvs),
141            ConfIOReason::NoFormatEnabled => RunReason::from_conf(),
142        }
143    }
144}