Skip to main content

ruststream_sqs_sns/
error.rs

1//! The crate-level error type.
2
3use std::error::Error as StdError;
4
5/// Errors returned by the Amazon SQS/SNS broker.
6///
7/// One enum for the whole crate, variants by source, per the `RustStream` broker conventions.
8/// The wrapped sources are boxed `std` errors so the public API does not leak the SDK's
9/// layered error types (they are formatted with their full cause chain).
10#[derive(Debug, thiserror::Error)]
11#[non_exhaustive]
12pub enum SqsError {
13    /// Loading the AWS configuration failed.
14    #[error("aws config error: {0}")]
15    Config(String),
16
17    /// Resolving a queue name to its URL (or creating the queue) failed.
18    #[error("sqs queue error for '{name}': {source}")]
19    Queue {
20        /// The queue name or URL the call was about.
21        name: String,
22        /// The SDK's failure, with its cause chain.
23        #[source]
24        source: Box<dyn StdError + Send + Sync>,
25    },
26
27    /// Receiving from a queue failed.
28    #[error("sqs receive error on '{queue}': {source}")]
29    Receive {
30        /// The queue URL the receive targeted.
31        queue: String,
32        /// The SDK's failure, with its cause chain.
33        #[source]
34        source: Box<dyn StdError + Send + Sync>,
35    },
36
37    /// Sending a message (SQS) or publishing a notification (SNS) failed.
38    #[error("publish error to '{destination}': {source}")]
39    Publish {
40        /// The queue or topic the message targeted.
41        destination: String,
42        /// The SDK's failure, with its cause chain.
43        #[source]
44        source: Box<dyn StdError + Send + Sync>,
45    },
46
47    /// A topic admin call (create, subscribe) failed.
48    #[error("sns admin error for '{name}': {source}")]
49    Admin {
50        /// The topic or subscription the call was about.
51        name: String,
52        /// The SDK's failure, with its cause chain.
53        #[source]
54        source: Box<dyn StdError + Send + Sync>,
55    },
56
57    /// The handle is used before `connect` filled the shared connection, or after `shutdown`.
58    #[error("sqs broker is not connected")]
59    NotConnected,
60
61    /// A queue descriptor is invalid.
62    #[error("invalid sqs queue descriptor: {0}")]
63    InvalidQueue(String),
64}
65
66/// Formats an SDK error with its full cause chain and boxes it, so transport failures stay
67/// distinguishable from service errors in logs (`DisplayErrorContext` walks the chain).
68pub(crate) fn sdk_err<E, R>(
69    err: &aws_sdk_sqs::error::SdkError<E, R>,
70) -> Box<dyn StdError + Send + Sync>
71where
72    E: StdError + Send + Sync + 'static,
73    R: std::fmt::Debug + Send + Sync + 'static,
74{
75    Box::from(aws_sdk_sqs::error::DisplayErrorContext(err).to_string())
76}