switchgear_pingora/
error.rs

1use std::borrow::Cow;
2use std::fmt::{Display, Formatter};
3use switchgear_service::api::service::{HasServiceErrorSource, ServiceErrorSource};
4use switchgear_service::components::discovery::error::DiscoveryBackendStoreError;
5use switchgear_service::components::pool::error::LnPoolError;
6use thiserror::Error;
7
8#[derive(Error, Debug)]
9pub enum PingoraLnErrorSourceKind {
10    #[error("{0}")]
11    PoolError(LnPoolError),
12    #[error("{0}")]
13    DiscoveryBackendStoreError(DiscoveryBackendStoreError),
14    #[error("no available lightning nodes")]
15    NoAvailableNodes,
16    #[error("{0}")]
17    IoError(std::io::Error),
18}
19
20#[derive(Error, Debug)]
21pub struct PingoraLnError {
22    context: Cow<'static, str>,
23    #[source]
24    source: PingoraLnErrorSourceKind,
25    esource: ServiceErrorSource,
26}
27
28impl Display for PingoraLnError {
29    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
30        write!(
31            f,
32            "PingoraLnError: while {}: {}",
33            self.context.as_ref(),
34            self.source
35        )
36    }
37}
38
39impl PingoraLnError {
40    pub fn new<C: Into<Cow<'static, str>>>(
41        source: PingoraLnErrorSourceKind,
42        esource: ServiceErrorSource,
43        context: C,
44    ) -> Self {
45        Self {
46            context: context.into(),
47            source,
48            esource,
49        }
50    }
51
52    pub fn from_pool_err<C: Into<Cow<'static, str>>>(
53        esource: ServiceErrorSource,
54        context: C,
55        error: LnPoolError,
56    ) -> Self {
57        Self {
58            context: context.into(),
59            source: PingoraLnErrorSourceKind::PoolError(error),
60            esource,
61        }
62    }
63
64    pub fn from_discovery_backend_store_err<C: Into<Cow<'static, str>>>(
65        esource: ServiceErrorSource,
66        context: C,
67        error: DiscoveryBackendStoreError,
68    ) -> Self {
69        Self {
70            context: context.into(),
71            source: PingoraLnErrorSourceKind::DiscoveryBackendStoreError(error),
72            esource,
73        }
74    }
75
76    pub fn no_available_nodes<C: Into<Cow<'static, str>>>(
77        esource: ServiceErrorSource,
78        context: C,
79    ) -> Self {
80        Self {
81            context: context.into(),
82            source: PingoraLnErrorSourceKind::NoAvailableNodes,
83            esource,
84        }
85    }
86
87    pub fn from_io_err<C: Into<Cow<'static, str>>>(
88        esource: ServiceErrorSource,
89        context: C,
90        error: std::io::Error,
91    ) -> Self {
92        Self {
93            context: context.into(),
94            source: PingoraLnErrorSourceKind::IoError(error),
95            esource,
96        }
97    }
98
99    /// Get the context message
100    pub fn context(&self) -> &str {
101        self.context.as_ref()
102    }
103
104    /// Get the error source
105    pub fn source(&self) -> &PingoraLnErrorSourceKind {
106        &self.source
107    }
108
109    /// Get the service error source
110    pub fn esource(&self) -> ServiceErrorSource {
111        self.esource
112    }
113}
114
115impl HasServiceErrorSource for PingoraLnError {
116    fn get_service_error_source(&self) -> ServiceErrorSource {
117        self.esource
118    }
119}