1use crate::parse_error::OMLCodeReason;
2use crate::{ConfReason, config_error::ConfCore};
3use derive_more::From;
4use orion_error::{ConfErrReason, ErrorCode, StructError, UvsFrom, 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(_: ConfReason<ConfCore>) -> Self {
52 Self::from_conf_reason(ConfErrReason::Core)
53 }
54}
55impl From<OMLCodeReason> for RunReason {
56 fn from(_: OMLCodeReason) -> Self {
57 Self::from_conf()
58 }
59}
60
61impl From<OrionSecReason> for RunReason {
62 fn from(value: OrionSecReason) -> Self {
63 match value {
64 OrionSecReason::Sec(_) => Self::from_conf(),
65 OrionSecReason::Uvs(uvs_reason) => Self::Uvs(uvs_reason),
66 }
67 }
68}
69
70pub trait RunErrorOwe<T> {
71 fn owe_sink(self) -> RunResult<T>;
72 fn owe_source(self) -> RunResult<T>;
73}
74
75impl<T, E> RunErrorOwe<T> for Result<T, E>
76where
77 E: std::fmt::Display,
78{
79 fn owe_sink(self) -> RunResult<T> {
80 match self {
81 Ok(v) => Ok(v),
82 Err(e) => Err(StructError::from(RunReason::Dist(DistFocus::SinkError(
83 "sink fail".into(),
84 )))
85 .with_detail(e.to_string())),
86 }
87 }
88 fn owe_source(self) -> RunResult<T> {
89 match self {
90 Ok(v) => Ok(v),
91 Err(e) => Err(
92 StructError::from(RunReason::Source(SourceFocus::SupplierError(
93 "source fail".into(),
94 )))
95 .with_detail(e.to_string()),
96 ),
97 }
98 }
99}
100
101impl From<SourceReason> for RunReason {
115 fn from(e: SourceReason) -> Self {
116 match e {
117 SourceReason::NotData => Self::Source(SourceFocus::NoData),
118 SourceReason::EOF => Self::Source(SourceFocus::Eof),
119 SourceReason::SupplierError(info) => Self::Source(SourceFocus::SupplierError(info)),
120 SourceReason::Disconnect(info) => Self::Source(SourceFocus::Disconnect(info)),
121 SourceReason::Other(info) => Self::Source(SourceFocus::Other(info)),
122 SourceReason::Uvs(uvs) => Self::Uvs(uvs),
123 }
124 }
126}
127
128impl From<SinkReason> for RunReason {
129 fn from(e: SinkReason) -> Self {
130 match e {
131 SinkReason::Sink(info) => Self::Dist(DistFocus::SinkError(info)),
132 SinkReason::Mock => Self::Dist(DistFocus::StgCtrl),
134 SinkReason::StgCtrl => Self::Dist(DistFocus::StgCtrl),
135 SinkReason::Uvs(uvs) => Self::Uvs(uvs),
136 }
137 }
138}
139use orion_conf::error::ConfIOReason;
140use wp_connector_api::{SinkReason, SourceReason};
141impl From<ConfIOReason> for RunReason {
142 fn from(value: ConfIOReason) -> Self {
143 match value {
144 ConfIOReason::Other(_) => RunReason::from_conf(),
145 ConfIOReason::Uvs(uvs) => RunReason::Uvs(uvs),
146 ConfIOReason::NoFormatEnabled => RunReason::from_conf(),
147 }
148 }
149}