Skip to main content

ruststream_gcp_pubsub/
error.rs

1//! The crate-level error type.
2
3use std::error::Error as StdError;
4
5/// Errors returned by the Google Cloud Pub/Sub 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 client's
9/// error types.
10#[derive(Debug, thiserror::Error)]
11#[non_exhaustive]
12pub enum PubSubError {
13    /// Building a client (authentication or connection setup) failed.
14    #[error("pubsub client error: {0}")]
15    Connect(#[source] Box<dyn StdError + Send + Sync>),
16
17    /// A topic or subscription admin call failed.
18    #[error("pubsub admin error for '{name}': {source}")]
19    Admin {
20        /// The resource the call was about.
21        name: String,
22        /// The client's failure.
23        #[source]
24        source: Box<dyn StdError + Send + Sync>,
25    },
26
27    /// The streaming pull failed permanently (transient failures are retried by the client).
28    #[error("pubsub receive error on '{subscription}': {source}")]
29    Receive {
30        /// The subscription the stream was pulling from.
31        subscription: String,
32        /// The client's failure.
33        #[source]
34        source: Box<dyn StdError + Send + Sync>,
35    },
36
37    /// Publishing to a topic failed.
38    #[error("pubsub publish error to '{topic}': {source}")]
39    Publish {
40        /// The topic the message was published to.
41        topic: String,
42        /// The client's failure.
43        #[source]
44        source: Box<dyn StdError + Send + Sync>,
45    },
46
47    /// The handle is used before `connect` filled the shared connection, or after `shutdown`.
48    #[error("pubsub broker is not connected")]
49    NotConnected,
50
51    /// A subscription descriptor is invalid.
52    #[error("invalid pubsub subscription descriptor: {0}")]
53    InvalidDescriptor(String),
54}
55
56/// Boxes a client error into the crate's `Box<dyn StdError>` source form.
57pub(crate) fn box_err<E>(err: E) -> Box<dyn StdError + Send + Sync>
58where
59    E: StdError + Send + Sync + 'static,
60{
61    Box::new(err)
62}