1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#![deny(warnings)]
#![deny(clippy::dbg_macro)]
use std::fmt::Debug;

mod alerts;
mod dispatch;
mod panic_handler;
pub mod prelude;
mod utils;

pub use dispatch::configure_pagerduty;
use serde_json::Value;

pub trait AirbagResult<E>: Sized {
    fn airbag_drop(self) {
        drop(self.airbag())
    }

    fn airbag_if<F: Fn(&E) -> bool>(self, f: F) -> Self;

    fn airbag(self) -> Self;
}

impl<T, E: Debug + 'static> AirbagResult<E> for Result<T, E> {
    fn airbag(self) -> Self {
        if let Err(e) = &self {
            crate::dispatch::HUB
                .read()
                .dispatch(|| crate::alerts::generate_error_alert(e));
        }
        self
    }

    fn airbag_if<F: Fn(&E) -> bool>(self, f: F) -> Self {
        if let Err(e) = &self {
            if f(e) {
                return self.airbag();
            }
        }
        self
    }
}

pub fn create_alert(summary: impl Into<String>, details: Option<Value>, dedup_key: Option<String>) {
    let summary = summary.into();
    crate::dispatch::HUB.read().dispatch(move || {
        crate::alerts::generate_message_alert(summary.clone(), details.clone(), dedup_key.clone())
    });
}