aws_smithy_observability/
error.rs

1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6//! Observability Errors
7
8use std::fmt;
9
10use aws_smithy_runtime_api::box_error::BoxError;
11
12/// An error in the SDKs Observability providers
13#[non_exhaustive]
14#[derive(Debug)]
15pub struct ObservabilityError {
16    kind: ErrorKind,
17    source: BoxError,
18}
19
20/// The types of errors associated with [ObservabilityError]
21#[non_exhaustive]
22#[derive(Debug)]
23pub enum ErrorKind {
24    /// A custom error that does not fall under any other error kind
25    Other,
26}
27
28impl ObservabilityError {
29    /// Create a new [`ObservabilityError`] from an [ErrorKind] and a [BoxError]
30    pub fn new<E>(kind: ErrorKind, err: E) -> Self
31    where
32        E: Into<BoxError>,
33    {
34        Self {
35            kind,
36            source: err.into(),
37        }
38    }
39
40    /// Returns the corresponding [`ErrorKind`] for this error.
41    pub fn kind(&self) -> &ErrorKind {
42        &self.kind
43    }
44}
45
46impl fmt::Display for ObservabilityError {
47    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48        match &self.kind {
49            ErrorKind::Other => write!(f, "unclassified error"),
50        }
51    }
52}
53
54impl std::error::Error for ObservabilityError {
55    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
56        Some(self.source.as_ref())
57    }
58}
59
60/// An simple error to represent issues with the global [crate::TelemetryProvider].
61#[non_exhaustive]
62#[derive(Debug)]
63pub struct GlobalTelemetryProviderError {
64    reason: &'static str,
65}
66
67impl GlobalTelemetryProviderError {
68    /// Create a new [GlobalTelemetryProviderError] with a given reason for the error.
69    pub fn new(reason: &'static str) -> Self {
70        Self { reason }
71    }
72}
73
74impl std::error::Error for GlobalTelemetryProviderError {}
75
76impl fmt::Display for GlobalTelemetryProviderError {
77    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
78        write!(f, "GlobalTelemetryProviderError: {}", self.reason)
79    }
80}