Skip to main content

ruststream_kinesis/
error.rs

1//! The crate-level error type.
2
3use std::error::Error as StdError;
4
5/// Errors returned by the Amazon Kinesis Data Streams 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 formatted with their full cause chain, so the
9/// public API does not leak the SDK's layered error types.
10#[derive(Debug, thiserror::Error)]
11#[non_exhaustive]
12pub enum KinesisError {
13    /// Loading the AWS configuration failed.
14    #[error("aws config error: {0}")]
15    Config(String),
16
17    /// A stream admin call (describe, create, shard listing) failed.
18    #[error("kinesis stream error for '{stream}': {source}")]
19    Stream {
20        /// The stream the call was about.
21        stream: String,
22        /// The SDK's failure, with its cause chain.
23        #[source]
24        source: Box<dyn StdError + Send + Sync>,
25    },
26
27    /// Reading a shard failed permanently.
28    #[error("kinesis read error on '{stream}' shard '{shard}': {source}")]
29    Read {
30        /// The stream the shard belongs to.
31        stream: String,
32        /// The shard the read targeted.
33        shard: String,
34        /// The SDK's failure, with its cause chain.
35        #[source]
36        source: Box<dyn StdError + Send + Sync>,
37    },
38
39    /// A delivered record is KPL-aggregated, which this crate does not deaggregate yet;
40    /// failing loudly beats handing the handler an opaque protobuf blob.
41    #[error("kinesis record on shard '{shard}' is KPL-aggregated (unsupported)")]
42    AggregatedRecord {
43        /// The shard the record arrived on.
44        shard: String,
45    },
46
47    /// Writing a record failed.
48    #[error("kinesis publish error to '{stream}': {source}")]
49    Publish {
50        /// The stream the record targeted.
51        stream: String,
52        /// The SDK's failure, with its cause chain.
53        #[source]
54        source: Box<dyn StdError + Send + Sync>,
55    },
56
57    /// The lease store failed.
58    #[error("kinesis lease store error for shard '{shard}': {source}")]
59    Lease {
60        /// The shard whose lease was involved.
61        shard: String,
62        /// The store's failure.
63        #[source]
64        source: Box<dyn StdError + Send + Sync>,
65    },
66
67    /// The handle is used before `connect` filled the shared connection, or after `shutdown`.
68    #[error("kinesis broker is not connected")]
69    NotConnected,
70
71    /// A stream descriptor is invalid.
72    #[error("invalid kinesis descriptor: {0}")]
73    Invalid(String),
74}
75
76/// Formats an SDK error with its full cause chain and boxes it.
77pub(crate) fn sdk_err<E, R>(
78    err: &aws_sdk_kinesis::error::SdkError<E, R>,
79) -> Box<dyn StdError + Send + Sync>
80where
81    E: StdError + Send + Sync + 'static,
82    R: std::fmt::Debug + Send + Sync + 'static,
83{
84    Box::from(aws_sdk_kinesis::error::DisplayErrorContext(err).to_string())
85}