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
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

//! Observability Errors

use std::fmt;

use aws_smithy_runtime_api::box_error::BoxError;

/// An error in the SDKs Observability providers
#[non_exhaustive]
#[derive(Debug)]
pub struct ObservabilityError {
    kind: ErrorKind,
    source: BoxError,
}

/// The types of errors associated with [ObservabilityError]
#[non_exhaustive]
#[derive(Debug)]
pub enum ErrorKind {
    /// An error setting the `GlobalTelemetryProvider``
    SettingGlobalProvider,
    /// Error flushing metrics pipeline
    MetricsFlush,
    /// Error gracefully shutting down Metrics Provider
    MetricsShutdown,
    /// A custom error that does not fall under any other error kind
    Other,
}

impl ObservabilityError {
    /// Create a new [`ObservabilityError`] from an [ErrorKind] and a [BoxError]
    pub fn new<E>(kind: ErrorKind, err: E) -> Self
    where
        E: Into<BoxError>,
    {
        Self {
            kind,
            source: err.into(),
        }
    }

    /// Returns the corresponding [`ErrorKind`] for this error.
    pub fn kind(&self) -> &ErrorKind {
        &self.kind
    }
}

impl fmt::Display for ObservabilityError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self.kind {
            ErrorKind::Other => write!(f, "unclassified error"),
            ErrorKind::SettingGlobalProvider => {
                write!(f, "failed to set global telemetry provider")
            }
            ErrorKind::MetricsFlush => write!(f, "failed to flush metrics pipeline"),
            ErrorKind::MetricsShutdown => write!(f, "failed to shutdown metrics provider"),
        }
    }
}

impl std::error::Error for ObservabilityError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        Some(self.source.as_ref())
    }
}