raws_error/
lib.rs

1use std::error::Error as StdError;
2
3use aws_smithy_types::error::display::DisplayErrorContext;
4use aws_smithy_types::error::metadata::ProvideErrorMetadata;
5use aws_smithy_types::error::ErrorMetadata;
6use miette::Diagnostic;
7use thiserror::Error;
8
9#[derive(Debug, Error)]
10#[error("RAWS CLI Error")]
11pub struct RawsError<E: AwsError> {
12    #[from]
13    source: E,
14}
15
16pub trait AwsError: ::std::error::Error + 'static {
17    type DisplayErrorContext<'a>;
18    fn error_context(&self) -> Self::DisplayErrorContext<'_>;
19
20    fn meta(&self) -> &ErrorMetadata;
21
22    fn code(&self) -> Option<&str> {
23        self.meta().code()
24    }
25
26    fn message(&self) -> Option<&str> {
27        self.meta().message()
28    }
29}
30
31impl<E: AwsError> Diagnostic for RawsError<E> {
32    fn code<'a>(&'a self) -> Option<Box<dyn std::fmt::Display + 'a>> {
33        match self.source.code() {
34            Some(text) => Some(Box::new(text)),
35            None => None,
36        }
37    }
38
39    fn help<'a>(&'a self) -> Option<Box<dyn std::fmt::Display + 'a>> {
40        match self.source.message() {
41            Some(text) => Some(Box::new(text)),
42            None => None,
43        }
44    }
45}
46
47impl<T: ProvideErrorMetadata + StdError + 'static> AwsError for T {
48    type DisplayErrorContext<'a> = DisplayErrorContext<&'a Self>;
49
50    fn error_context(&self) -> Self::DisplayErrorContext<'_> {
51        DisplayErrorContext(self)
52    }
53
54    fn meta(&self) -> &ErrorMetadata {
55        self.meta()
56    }
57}