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
//! Error handling module.
use std::error::Error;
use std::fmt::{Display, Formatter};

/// Broker error enum.
///
/// Includes two sub types:
/// 1. NetworkError: a wrapper around [ureq::Error] string representation, which would be from
/// making network requests or parsing return value to JSON.
/// 2. BrokerError: a String type returned from the BGPKIT Broker API.
#[derive(Debug)]
pub enum BrokerError {
    NetworkError(String),
    BrokerError(String),
}

impl Error for BrokerError {}

impl Display for BrokerError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            BrokerError::NetworkError(e) => {write!(f, "NETWORK_ERROR: {}", e)}
            BrokerError::BrokerError(e) => {write!(f, "BROKER_ERROR: {}", e)}
        }
    }
}

impl From<reqwest::Error> for BrokerError {
    fn from(e: reqwest::Error) -> Self {
        BrokerError::NetworkError(e.to_string())
    }
}

#[cfg(test)]
mod tests {
    use crate::{BgpkitBroker, CollectorLatestItem};

    #[test]
    fn test_anyhow() {

        fn test_error() -> anyhow::Result<Vec<CollectorLatestItem>> {
            Ok(BgpkitBroker::new().latest()?)
        }
        let _res = test_error();
    }
}