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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use gcloud_sdk::error::Error;
use opentelemetry::ExportError;
use rsb_derive::*;

pub type BoxedError = Box<dyn std::error::Error + Send + Sync>;

#[derive(Debug)]
pub enum GcloudTraceError {
    SystemError(GcloudTraceSystemError),
    NetworkError(GcloudTraceNetworkError),
}

impl std::fmt::Display for GcloudTraceError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match *self {
            GcloudTraceError::SystemError(ref err) => err.fmt(f),
            GcloudTraceError::NetworkError(ref err) => err.fmt(f),
        }
    }
}

impl std::error::Error for GcloudTraceError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match *self {
            GcloudTraceError::SystemError(ref err) => Some(err),
            GcloudTraceError::NetworkError(ref err) => Some(err),
        }
    }
}

#[derive(Debug, Builder)]
pub struct GcloudTraceSystemError {
    pub message: String,
    pub root_cause: Option<BoxedError>,
}

impl std::fmt::Display for GcloudTraceSystemError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "System error: {:?}", self.message)
    }
}

impl std::error::Error for GcloudTraceSystemError {}

#[derive(Debug, Eq, PartialEq, Clone, Builder)]
pub struct GcloudTraceNetworkError {
    pub message: String,
}

impl std::fmt::Display for GcloudTraceNetworkError {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "Network error: {}", self.message)
    }
}

impl std::error::Error for GcloudTraceNetworkError {}

impl From<gcloud_sdk::error::Error> for GcloudTraceError {
    fn from(gcloud_error: Error) -> Self {
        GcloudTraceError::SystemError(
            GcloudTraceSystemError::new(format!("Google SDK error: {gcloud_error}"))
                .with_root_cause(Box::new(gcloud_error)),
        )
    }
}

impl From<gcloud_sdk::tonic::Status> for GcloudTraceError {
    fn from(status: gcloud_sdk::tonic::Status) -> Self {
        GcloudTraceError::NetworkError(GcloudTraceNetworkError::new(format!("{status}")))
    }
}

impl ExportError for GcloudTraceError {
    fn exporter_name(&self) -> &'static str {
        "GoogleCloudTraceExporter"
    }
}