use crate::Error;
use std::sync::Arc;
#[derive(thiserror::Error, Debug)]
#[non_exhaustive]
pub enum PublishError {
#[error("the publish operation was interrupted by an error: {0}")]
Rpc(#[source] Arc<Error>),
#[error("publishing is paused for the ordering key")]
OrderingKeyPaused,
#[error("the publisher has shut down")]
Shutdown,
}
#[derive(thiserror::Error, Debug)]
#[non_exhaustive]
pub enum AckError {
#[error("the message's lease has already expired. It was not acked, and will be redelivered.")]
LeaseExpired,
#[non_exhaustive]
#[error("the operation failed. RPC error: {source}")]
Rpc {
#[source]
source: Arc<Error>,
},
#[error(
"shutdown before attempting the operation. \
The message was not acknowledged, and will be redelivered."
)]
ShutdownBeforeAck,
#[error("error during shutdown. The result of the operation is unknown. {0}")]
Shutdown(#[source] Box<dyn std::error::Error + Send + Sync + 'static>),
}
#[cfg(test)]
mod tests {
use super::*;
use google_cloud_gax::error::rpc::{Code, Status};
#[test]
fn ack_error_rpc_debug() {
let e = AckError::Rpc {
source: Arc::new(Error::service(
Status::default()
.set_code(Code::FailedPrecondition)
.set_message("inner fail"),
)),
};
let fmt = format!("{e}");
assert!(fmt.contains("operation failed."), "{fmt}");
assert!(fmt.contains("inner fail"), "{fmt}");
}
impl PartialEq for AckError {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
(AckError::LeaseExpired, AckError::LeaseExpired) => true,
(AckError::ShutdownBeforeAck, AckError::ShutdownBeforeAck) => true,
(AckError::Rpc { source: s1 }, AckError::Rpc { source: s2 }) => {
format!("{:?}", s1) == format!("{:?}", s2)
}
(AckError::Shutdown(e1), AckError::Shutdown(e2)) => {
format!("{:?}", e1) == format!("{:?}", e2)
}
_ => false,
}
}
}
}