risingwave_rpc_client/
error.rs1use risingwave_common::util::meta_addr::MetaAddressStrategyParseError;
16use risingwave_error::tonic::TonicStatusWrapperExt as _;
17use thiserror::Error;
18use thiserror_ext::Construct;
19
20pub type Result<T, E = RpcError> = std::result::Result<T, E>;
21
22pub use risingwave_error::tonic::{ToTonicStatus, TonicStatusWrapper};
24
25#[derive(Error, Debug, Construct)]
26pub enum RpcError {
27 #[error(transparent)]
28 TransportError(Box<tonic::transport::Error>),
29
30 #[error(transparent)]
31 GrpcStatus(
32 #[from]
33 #[backtrace]
37 Box<TonicStatusWrapper>,
38 ),
39
40 #[error(transparent)]
41 MetaAddressParse(#[from] MetaAddressStrategyParseError),
42
43 #[error(transparent)]
44 Internal(
45 #[from]
46 #[backtrace]
47 anyhow::Error,
48 ),
49}
50
51static_assertions::const_assert_eq!(std::mem::size_of::<RpcError>(), 32);
53
54impl From<tonic::transport::Error> for RpcError {
55 fn from(e: tonic::transport::Error) -> Self {
56 RpcError::TransportError(Box::new(e))
57 }
58}
59
60impl !From<tonic::Status> for RpcError {}
63
64macro_rules! impl_from_status {
65 ($($service:ident),* $(,)?) => {
66 paste::paste! {
67 impl RpcError {
68 $(
69 #[doc = "Convert a gRPC status from " $service " service into an [`RpcError`]."]
70 pub fn [<from_ $service _status>](s: tonic::Status) -> Self {
71 Box::new(s.with_client_side_service_name(stringify!($service))).into()
72 }
73 )*
74 }
75 }
76 };
77}
78
79impl_from_status!(stream, batch, meta, compute, compactor, connector, frontend);
80
81impl RpcError {
82 pub fn is_connection_error(&self) -> bool {
85 match self {
86 RpcError::TransportError(_) => true,
87 RpcError::GrpcStatus(status) => matches!(
88 status.inner().code(),
89 tonic::Code::Unavailable | tonic::Code::Unknown | tonic::Code::Unimplemented ),
93 RpcError::MetaAddressParse(_) => false,
94 RpcError::Internal(anyhow) => anyhow
95 .downcast_ref::<Self>() .is_some_and(Self::is_connection_error),
97 }
98 }
99}