aws_smithy_observability/
error.rs

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
78
79
80
/*
 * 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 {
    /// 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"),
        }
    }
}

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

/// An simple error to represent issues with the global [crate::TelemetryProvider].
#[non_exhaustive]
#[derive(Debug)]
pub struct GlobalTelemetryProviderError {
    reason: &'static str,
}

impl GlobalTelemetryProviderError {
    /// Create a new [GlobalTelemetryProviderError] with a given reason for the error.
    pub fn new(reason: &'static str) -> Self {
        Self { reason }
    }
}

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

impl fmt::Display for GlobalTelemetryProviderError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "GlobalTelemetryProviderError: {}", self.reason)
    }
}