diff --git a/tonic/src/lib.rs b/tonic/src/lib.rs index 56ad19fe0..fbb625029 100644 --- a/tonic/src/lib.rs +++ b/tonic/src/lib.rs @@ -120,7 +120,7 @@ pub use extensions::GrpcMethod; pub use http::Extensions; pub use request::{IntoRequest, IntoStreamingRequest, Request}; pub use response::Response; -pub use status::{Code, Status}; +pub use status::{Code, Status, TimeoutExpired}; pub(crate) type Error = Box; diff --git a/tonic/src/status.rs b/tonic/src/status.rs index 4f0c2bf90..2d09295d0 100644 --- a/tonic/src/status.rs +++ b/tonic/src/status.rs @@ -606,8 +606,7 @@ fn find_status_in_source_chain(err: &(dyn Error + 'static)) -> Option { }); } - #[cfg(feature = "server")] - if let Some(timeout) = err.downcast_ref::() { + if let Some(timeout) = err.downcast_ref::() { return Some(Status::cancelled(timeout.to_string())); } @@ -1013,3 +1012,23 @@ mod tests { assert_eq!(status.details(), DETAILS); } } + +/// Error returned if a request didn't complete within the configured timeout. +/// +/// Timeouts can be configured either with [`Endpoint::timeout`], [`Server::timeout`], or by +/// setting the [`grpc-timeout` metadata value][spec]. +/// +/// [`Endpoint::timeout`]: crate::transport::server::Server::timeout +/// [`Server::timeout`]: crate::transport::channel::Endpoint::timeout +/// [spec]: https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md +#[derive(Debug)] +pub struct TimeoutExpired(pub ()); + +impl fmt::Display for TimeoutExpired { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "Timeout expired") + } +} + +// std::error::Error only requires a type to impl Debug and Display +impl std::error::Error for TimeoutExpired {} diff --git a/tonic/src/transport/mod.rs b/tonic/src/transport/mod.rs index 254e5ef8d..3d3008b52 100644 --- a/tonic/src/transport/mod.rs +++ b/tonic/src/transport/mod.rs @@ -106,8 +106,6 @@ pub use self::error::Error; #[doc(inline)] #[cfg(feature = "server")] pub use self::server::Server; -#[doc(inline)] -pub use self::service::grpc_timeout::TimeoutExpired; #[cfg(feature = "tls")] pub use self::tls::Certificate; diff --git a/tonic/src/transport/service/grpc_timeout.rs b/tonic/src/transport/service/grpc_timeout.rs index 31843d930..73331fa31 100644 --- a/tonic/src/transport/service/grpc_timeout.rs +++ b/tonic/src/transport/service/grpc_timeout.rs @@ -1,8 +1,7 @@ -use crate::metadata::GRPC_TIMEOUT_HEADER; +use crate::{metadata::GRPC_TIMEOUT_HEADER, TimeoutExpired}; use http::{HeaderMap, HeaderValue, Request}; use pin_project::pin_project; use std::{ - fmt, future::Future, pin::Pin, task::{ready, Context, Poll}, @@ -147,26 +146,6 @@ fn try_parse_grpc_timeout( } } -/// Error returned if a request didn't complete within the configured timeout. -/// -/// Timeouts can be configured either with [`Endpoint::timeout`], [`Server::timeout`], or by -/// setting the [`grpc-timeout` metadata value][spec]. -/// -/// [`Endpoint::timeout`]: crate::transport::server::Server::timeout -/// [`Server::timeout`]: crate::transport::channel::Endpoint::timeout -/// [spec]: https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md -#[derive(Debug)] -pub struct TimeoutExpired(()); - -impl fmt::Display for TimeoutExpired { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "Timeout expired") - } -} - -// std::error::Error only requires a type to impl Debug and Display -impl std::error::Error for TimeoutExpired {} - #[cfg(test)] mod tests { use super::*;