pub_sub_client/error/
mod.rs1use reqwest::Response;
2use serde_json::Value;
3use std::{convert::identity, error::Error as StdError};
4use thiserror::Error;
5
6#[derive(Debug, Error)]
7pub enum Error {
8 #[error("initialization error: {reason}")]
9 Initialization {
10 reason: String,
11 source: Box<dyn StdError + Send + Sync + 'static>,
12 },
13
14 #[error("getting authentication token failed")]
15 TokenFetch(#[from] Box<goauth::GoErr>),
16
17 #[error("HTTP communication with Pub/Sub service failed")]
18 HttpServiceCommunication(#[source] reqwest::Error),
19 #[error("unexpected HTTP status code `{0}` from Pub/Sub service: {1}")]
20 UnexpectedHttpStatusCode(reqwest::StatusCode, String),
21 #[error("unexpected HTTP response from Pub/Sub service")]
22 UnexpectedHttpResponse(#[source] reqwest::Error),
23
24 #[error("decoding data of received message as Base64 failed")]
25 DecodeBase64(#[source] base64::DecodeError),
26 #[error("message contains no data")]
27 NoData,
28 #[error("deserializing data of received message failed")]
29 Deserialize(#[source] serde_json::Error),
30 #[error("serializing of message to be published failed")]
31 Serialize(#[source] serde_json::Error),
32 #[error("failed to transform JSON value")]
33 Transform(#[source] Box<dyn StdError + Send + Sync + 'static>),
34}
35
36impl Error {
37 pub async fn unexpected_http_status_code(response: Response) -> Error {
38 Error::UnexpectedHttpStatusCode(
39 response.status(),
40 response
41 .text()
42 .await
43 .map_err(|e| format!("failed to get response body as text: {e}"))
44 .and_then(|text| {
45 serde_json::from_str::<Value>(&text)
46 .map_err(|e| format!("failed to parse error response: {e}"))
47 .map(|v| v["error"]["message"].to_string())
48 })
49 .unwrap_or_else(identity),
50 )
51 }
52}