Skip to main content

ruststream_gcp_pubsub/
error.rs

1//! The crate-level error type.
2
3use std::error::Error as StdError;
4use std::time::Duration;
5
6/// Errors returned by the Google Cloud Pub/Sub broker.
7///
8/// One enum for the whole crate, variants by source, per the `RustStream` broker conventions.
9/// The wrapped sources are boxed `std` errors so the public API does not leak the client's
10/// error types.
11#[derive(Debug, thiserror::Error)]
12#[non_exhaustive]
13pub enum PubSubError {
14    /// Building a client (authentication or connection setup) failed.
15    #[error("pubsub client error: {0}")]
16    Connect(#[source] Box<dyn StdError + Send + Sync>),
17
18    /// A topic or subscription admin call failed.
19    #[error("pubsub admin error for '{name}': {source}")]
20    Admin {
21        /// The resource the call was about.
22        name: String,
23        /// The client's failure.
24        #[source]
25        source: Box<dyn StdError + Send + Sync>,
26    },
27
28    /// The streaming pull failed permanently (transient failures are retried by the client).
29    #[error("pubsub receive error on '{subscription}': {source}")]
30    Receive {
31        /// The subscription the stream was pulling from.
32        subscription: String,
33        /// The client's failure.
34        #[source]
35        source: Box<dyn StdError + Send + Sync>,
36    },
37
38    /// Publishing to a topic failed.
39    #[error("pubsub publish error to '{topic}': {source}")]
40    Publish {
41        /// The topic the message was published to.
42        topic: String,
43        /// The client's failure.
44        #[source]
45        source: Box<dyn StdError + Send + Sync>,
46    },
47
48    /// The handle is used before `connect` filled the shared connection, or after `shutdown`.
49    #[error("pubsub broker is not connected")]
50    NotConnected,
51
52    /// A subscription descriptor is invalid.
53    #[error("invalid pubsub subscription descriptor: {0}")]
54    InvalidDescriptor(String),
55
56    /// A delayed retry asks to hold a delivery for longer than the subscription keeps it leased.
57    #[error(
58        "a delayed retry of {requested:?} outlives the subscription's maximum lease of {lease:?}; \
59         the delivery would come back before the delay elapsed. Raise it with \
60         GooglePubSub::max_lease, or ask for a shorter delay"
61    )]
62    DelayBeyondLease {
63        /// The delay the handler asked for.
64        requested: Duration,
65        /// How long the client keeps a delivery leased.
66        lease: Duration,
67    },
68}
69
70/// Boxes a client error into the crate's `Box<dyn StdError>` source form.
71pub(crate) fn box_err<E>(err: E) -> Box<dyn StdError + Send + Sync>
72where
73    E: StdError + Send + Sync + 'static,
74{
75    Box::new(err)
76}