wp_error/
run_error.rs

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