pub struct ConnectError {
pub code: ErrorCode,
pub message: Option<String>,
pub details: Vec<ErrorDetail>,
/* private fields */
}Expand description
The Connect transport error DialError::Connect wraps, re-exported for
the same reason ErrorCode is: an edge that maps a dial failure onto its
own surface’s error taxonomy needs to name the type it is mapping from.
A ConnectRPC error.
Fields§
§code: ErrorCodeThe error code.
message: Option<String>Human-readable error message.
details: Vec<ErrorDetail>Additional error details.
Implementations§
Source§impl ConnectError
impl ConnectError
Sourcepub fn new(code: ErrorCode, message: impl Into<String>) -> ConnectError
pub fn new(code: ErrorCode, message: impl Into<String>) -> ConnectError
Create a new error with the given code and message.
Sourcepub fn with_headers(self, headers: HeaderMap) -> ConnectError
pub fn with_headers(self, headers: HeaderMap) -> ConnectError
Add response headers to be included in the error response.
Sourcepub fn with_trailers(self, trailers: HeaderMap) -> ConnectError
pub fn with_trailers(self, trailers: HeaderMap) -> ConnectError
Add response trailers to be included in the error response.
Sourcepub fn response_headers(&self) -> &HeaderMap
pub fn response_headers(&self) -> &HeaderMap
Borrow the response headers. Returns an empty map if none were set.
Sourcepub fn trailers(&self) -> &HeaderMap
pub fn trailers(&self) -> &HeaderMap
Borrow the response trailers. Returns an empty map if none were set.
Sourcepub fn response_headers_mut(&mut self) -> &mut HeaderMap
pub fn response_headers_mut(&mut self) -> &mut HeaderMap
Mutably borrow the response headers, allocating an empty map if none were set.
Sourcepub fn trailers_mut(&mut self) -> &mut HeaderMap
pub fn trailers_mut(&mut self) -> &mut HeaderMap
Mutably borrow the response trailers, allocating an empty map if none were set.
Sourcepub fn set_response_headers(&mut self, headers: HeaderMap)
pub fn set_response_headers(&mut self, headers: HeaderMap)
Replace the response headers. An empty map is stored as None.
Sourcepub fn set_trailers(&mut self, trailers: HeaderMap)
pub fn set_trailers(&mut self, trailers: HeaderMap)
Replace the response trailers. An empty map is stored as None.
Sourcepub fn with_http_status(self, status: StatusCode) -> ConnectError
pub fn with_http_status(self, status: StatusCode) -> ConnectError
Set an HTTP status override for this error.
When set, this overrides the default HTTP status derived from the error code. This is useful for HTTP-level errors like 415 Unsupported Media Type.
Sourcepub fn unsupported_media_type(message: impl Into<String>) -> ConnectError
pub fn unsupported_media_type(message: impl Into<String>) -> ConnectError
Create an error for unsupported media type (HTTP 415).
This is used when the client sends a content type that the server doesn’t support.
Sourcepub fn method_not_allowed(message: impl Into<String>) -> ConnectError
pub fn method_not_allowed(message: impl Into<String>) -> ConnectError
Create an error for method not allowed (HTTP 405).
This is used when the client uses an HTTP method other than POST.
Sourcepub fn canceled(message: impl Into<String>) -> ConnectError
pub fn canceled(message: impl Into<String>) -> ConnectError
Create a canceled error.
Sourcepub fn unknown(message: impl Into<String>) -> ConnectError
pub fn unknown(message: impl Into<String>) -> ConnectError
Create an unknown error.
Sourcepub fn invalid_argument(message: impl Into<String>) -> ConnectError
pub fn invalid_argument(message: impl Into<String>) -> ConnectError
Create an invalid argument error.
Sourcepub fn deadline_exceeded(message: impl Into<String>) -> ConnectError
pub fn deadline_exceeded(message: impl Into<String>) -> ConnectError
Create a deadline exceeded error.
Sourcepub fn not_found(message: impl Into<String>) -> ConnectError
pub fn not_found(message: impl Into<String>) -> ConnectError
Create a not found error.
Sourcepub fn already_exists(message: impl Into<String>) -> ConnectError
pub fn already_exists(message: impl Into<String>) -> ConnectError
Create an already exists error.
Sourcepub fn permission_denied(message: impl Into<String>) -> ConnectError
pub fn permission_denied(message: impl Into<String>) -> ConnectError
Create a permission denied error.
Sourcepub fn resource_exhausted(message: impl Into<String>) -> ConnectError
pub fn resource_exhausted(message: impl Into<String>) -> ConnectError
Create a resource exhausted error.
Sourcepub fn failed_precondition(message: impl Into<String>) -> ConnectError
pub fn failed_precondition(message: impl Into<String>) -> ConnectError
Create a failed precondition error.
Sourcepub fn aborted(message: impl Into<String>) -> ConnectError
pub fn aborted(message: impl Into<String>) -> ConnectError
Create an aborted error.
Sourcepub fn out_of_range(message: impl Into<String>) -> ConnectError
pub fn out_of_range(message: impl Into<String>) -> ConnectError
Create an out of range error.
Sourcepub fn unimplemented(message: impl Into<String>) -> ConnectError
pub fn unimplemented(message: impl Into<String>) -> ConnectError
Create an unimplemented error.
Sourcepub fn internal(message: impl Into<String>) -> ConnectError
pub fn internal(message: impl Into<String>) -> ConnectError
Create an internal error.
Create an unavailable error.
Sourcepub fn data_loss(message: impl Into<String>) -> ConnectError
pub fn data_loss(message: impl Into<String>) -> ConnectError
Create a data loss error.
Sourcepub fn unauthenticated(message: impl Into<String>) -> ConnectError
pub fn unauthenticated(message: impl Into<String>) -> ConnectError
Create an unauthenticated error.
Sourcepub fn with_detail(self, detail: ErrorDetail) -> ConnectError
pub fn with_detail(self, detail: ErrorDetail) -> ConnectError
Add an error detail.
Sourcepub fn http_status(&self) -> StatusCode
pub fn http_status(&self) -> StatusCode
Get the HTTP status code for this error.
Returns the HTTP status override if set, otherwise derives it from the error code.
Source§impl ConnectError
impl ConnectError
Sourcepub fn into_http_response(
self,
request_headers: &HeaderMap,
) -> Response<ConnectRpcBody>
pub fn into_http_response( self, request_headers: &HeaderMap, ) -> Response<ConnectRpcBody>
Render this error as a complete HTTP response in the wire format matching the inbound request’s protocol.
This is the building block for tower::Layers that short-circuit a
request (auth, rate limiting, validation) before it reaches
ConnectRpcService. A layer cannot reuse the service’s internal error
rendering, but still needs to produce a response that the calling
client will decode as a structured error rather than a transport
failure. Each protocol expects a different wire shape:
- Connect unary (
application/proto,application/json, or absent — Connect GET requests carry no request body orContent-Type): a non-200 HTTP status with a JSON error body. Connect unary error bodies are spec-required JSON regardless of the request codec. - Connect streaming (
application/connect+{proto,json}): HTTP 200 with the error in anEndStreamResponseenvelope. - gRPC / gRPC-Web (
application/grpc*): HTTP 200 withgrpc-statusandgrpc-messagetrailers (as HTTP/2 trailers for gRPC, encoded in the body for gRPC-Web).
The protocol is detected from request_headers via
Protocol::detect. When detection fails — an unrecognized
Content-Type, or none at all — the Connect unary JSON shape is used,
which is the most universally parseable fallback.
gRPC and gRPC-Web use the same framed wire shape for unary and streaming calls, so the trailers-only response is always correct regardless of the method’s cardinality.
§Example
use connectrpc::ConnectError;
// Inside a tower::Service::call wrapping ConnectRpcService:
fn call(&mut self, req: http::Request<B>) -> Self::Future {
if !self.is_authorized(&req) {
let resp = ConnectError::permission_denied("access denied")
.into_http_response(req.headers());
return Box::pin(std::future::ready(Ok(resp)));
}
// ... call inner service ...
}Trait Implementations§
Source§impl Clone for ConnectError
impl Clone for ConnectError
Source§fn clone(&self) -> ConnectError
fn clone(&self) -> ConnectError
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for ConnectError
impl Debug for ConnectError
Source§impl<'de> Deserialize<'de> for ConnectError
impl<'de> Deserialize<'de> for ConnectError
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<ConnectError, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<ConnectError, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Source§impl Display for ConnectError
impl Display for ConnectError
Source§impl Error for ConnectError
impl Error for ConnectError
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()
Source§impl From<ConnectError> for DialError
impl From<ConnectError> for DialError
Source§fn from(source: ConnectError) -> Self
fn from(source: ConnectError) -> Self
Source§impl From<Error> for ConnectError
impl From<Error> for ConnectError
Source§fn from(err: Error) -> ConnectError
fn from(err: Error) -> ConnectError
Source§impl From<Error> for ConnectError
Lets Response::try_with_header(..)? propagate naturally inside a
handler.
impl From<Error> for ConnectError
Lets Response::try_with_header(..)? propagate naturally inside a
handler.
Source§fn from(err: Error) -> ConnectError
fn from(err: Error) -> ConnectError
Source§impl IntoResponse for ConnectError
impl IntoResponse for ConnectError
Source§fn into_response(self) -> Response<Body>
fn into_response(self) -> Response<Body>
Source§impl Serialize for ConnectError
impl Serialize for ConnectError
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
Auto Trait Implementations§
impl Freeze for ConnectError
impl RefUnwindSafe for ConnectError
impl Send for ConnectError
impl Sync for ConnectError
impl Unpin for ConnectError
impl UnsafeUnpin for ConnectError
impl UnwindSafe for ConnectError
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
Source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
Source§impl<T, S> Handler<IntoResponseHandler, S> for T
impl<T, S> Handler<IntoResponseHandler, S> for T
Source§fn call(
self,
_req: Request<Body>,
_state: S,
) -> <T as Handler<IntoResponseHandler, S>>::Future
fn call( self, _req: Request<Body>, _state: S, ) -> <T as Handler<IntoResponseHandler, S>>::Future
Source§fn layer<L>(self, layer: L) -> Layered<L, Self, T, S>where
L: Layer<HandlerService<Self, T, S>> + Clone,
<L as Layer<HandlerService<Self, T, S>>>::Service: Service<Request<Body>>,
fn layer<L>(self, layer: L) -> Layered<L, Self, T, S>where
L: Layer<HandlerService<Self, T, S>> + Clone,
<L as Layer<HandlerService<Self, T, S>>>::Service: Service<Request<Body>>,
tower::Layer to the handler. Read moreSource§fn with_state(self, state: S) -> HandlerService<Self, T, S>
fn with_state(self, state: S) -> HandlerService<Self, T, S>
Service by providing the stateSource§impl<H, T> HandlerWithoutStateExt<T> for H
impl<H, T> HandlerWithoutStateExt<T> for H
Source§fn into_service(self) -> HandlerService<H, T, ()>
fn into_service(self) -> HandlerService<H, T, ()>
Service and no state.Source§fn into_make_service(self) -> IntoMakeService<HandlerService<H, T, ()>>
fn into_make_service(self) -> IntoMakeService<HandlerService<H, T, ()>>
MakeService and no state. Read moreSource§fn into_make_service_with_connect_info<C>(
self,
) -> IntoMakeServiceWithConnectInfo<HandlerService<H, T, ()>, C>
fn into_make_service_with_connect_info<C>( self, ) -> IntoMakeServiceWithConnectInfo<HandlerService<H, T, ()>, C>
MakeService which stores information
about the incoming connection and has no state. Read moreSource§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request