use reqwest::Client;
use std::future::Future;
use std::pin::Pin;
use super::ErrorEvent;
#[derive(Debug, thiserror::Error)]
pub enum NotifyError {
#[error("transient error: {0}")]
Transient(String),
#[error("permanent error: {0}")]
Permanent(String),
}
impl NotifyError {
pub fn transient(err: impl std::fmt::Display) -> Self {
Self::Transient(err.to_string())
}
pub fn permanent(err: impl std::fmt::Display) -> Self {
Self::Permanent(err.to_string())
}
}
impl From<reqwest::Error> for NotifyError {
fn from(err: reqwest::Error) -> Self {
if err.is_timeout() || err.is_connect() {
Self::Transient(err.to_string())
} else if err.is_status() {
if err.status().is_some_and(|s| s.is_server_error()) {
Self::Transient(err.to_string())
} else {
Self::Permanent(err.to_string())
}
} else {
Self::Transient(err.to_string())
}
}
}
pub type NotifyFuture<'a> = Pin<Box<dyn Future<Output = Result<(), NotifyError>> + Send + 'a>>;
pub trait ErrorNotifier: Send + Sync {
fn notify<'a>(&'a self, client: &'a Client, event: &'a ErrorEvent) -> NotifyFuture<'a>;
fn name(&self) -> &'static str;
}