mod errors;
#[cfg(feature = "notify")]
mod std_handler;
#[cfg(feature = "notify")]
pub use std_handler::{AsyncHandlerRegistration, SyncHandlerRegistration};
#[cfg(feature = "notify-tokio")]
mod tokio_handler;
#[cfg(feature = "notify-tokio")]
pub use tokio_handler::TokioAsyncHandlerRegistration;
use crate::Err;
use chrono::{DateTime, Utc};
use std::sync;
#[cfg(feature = "notify-tokio")]
use std::future::Future;
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum ErrHandlingErrorKind {
StdMutexIsPoisoned,
InvalidInternalState,
InvalidCallTiming,
}
#[derive(Debug)]
pub struct ErrHandlingError {
kind: ErrHandlingErrorKind,
}
#[cfg(feature = "notify")]
#[cfg_attr(docsrs, doc(cfg(feature = "notify")))]
pub fn add_async_err_handler<F>(handler: F) -> Result<(), ErrHandlingError>
where
F: Fn(&Err, DateTime<Utc>) + Send + Sync + 'static,
{
std_handler::add_async_handler(&std_handler::HANDLERS, handler)
}
#[cfg(feature = "notify")]
#[cfg_attr(docsrs, doc(cfg(feature = "notify")))]
pub fn add_sync_err_handler<F>(handler: F) -> Result<(), ErrHandlingError>
where
F: Fn(&Err, DateTime<Utc>) + Send + Sync + 'static,
{
std_handler::add_sync_handler(&std_handler::HANDLERS, handler)
}
#[cfg(feature = "notify-tokio")]
#[cfg_attr(docsrs, doc(cfg(feature = "notify-tokio")))]
pub fn add_tokio_async_err_handler<F, Fut>(handler: F) -> Result<(), ErrHandlingError>
where
F: Fn(sync::Arc<Err>, DateTime<Utc>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = ()> + Send + 'static,
{
tokio_handler::add_tokio_async_handler(&tokio_handler::HANDLERS, handler)
}
pub fn fix_err_handlers() -> Result<(), ErrHandlingError> {
#[cfg(feature = "notify")]
let result_std = std_handler::fix_handlers(&std_handler::HANDLERS);
#[cfg(feature = "notify-tokio")]
let result_tokio = tokio_handler::fix_handlers(&tokio_handler::HANDLERS);
#[cfg(feature = "notify")]
result_std?;
#[cfg(feature = "notify-tokio")]
result_tokio?;
Ok(())
}
pub(crate) fn notify_err(err: Err) -> Result<(), ErrHandlingError> {
let tm = Utc::now();
let err = sync::Arc::new(err);
#[cfg(feature = "notify")]
let result_std = std_handler::handle_err(&std_handler::HANDLERS, sync::Arc::clone(&err), tm);
#[cfg(feature = "notify-tokio")]
let result_tokio =
tokio_handler::handle_err(&tokio_handler::HANDLERS, sync::Arc::clone(&err), tm);
#[cfg(feature = "notify")]
result_std?;
#[cfg(feature = "notify-tokio")]
result_tokio?;
Ok(())
}