pub struct RestError(/* private fields */);Expand description
REST error wrapper — converts tonic::Status to an HTTP error response.
Maps gRPC status codes to HTTP status codes and returns a JSON error body following the Google API error model:
{
"error": { "code": 400, "message": "...", "status": "INVALID_ARGUMENT" }
}§Response Format
The JSON shape is intentionally fixed to the Google API error convention.
This provides a consistent, well-documented error format across all generated
REST endpoints. The body wraps the error in an "error" object:
{ "error": { "code": 404, "message": "...", "status": "NOT_FOUND" } }Note: SSE error events (via sse_error_event) use
the same {"error": {...}} format, ensuring a consistent error shape across
both HTTP JSON and SSE transports.
If you need a custom error shape, implement
axum::response::IntoResponse on your own error type and set the
runtime_crate config in tonic-rest-build to point to the module
containing your custom types.
§Constructing
Use From<tonic::Status> or RestError::new:
let err = RestError::new(tonic::Status::not_found("gone"));
let err: RestError = tonic::Status::not_found("gone").into();§Examples
Convert a tonic status to an Axum-compatible HTTP response:
use tonic_rest::RestError;
use axum::response::IntoResponse;
let err = RestError::new(tonic::Status::not_found("user not found"));
let response = err.into_response();
assert_eq!(response.status(), axum::http::StatusCode::NOT_FOUND);Implementations§
Source§impl RestError
impl RestError
Sourcepub const fn new(status: Status) -> Self
pub const fn new(status: Status) -> Self
Create a new RestError from a tonic::Status.
Sourcepub const fn status(&self) -> &Status
pub const fn status(&self) -> &Status
Returns a reference to the underlying tonic::Status.
Sourcepub fn into_status(self) -> Status
pub fn into_status(self) -> Status
Consumes the RestError and returns the underlying tonic::Status.
Trait Implementations§
Source§impl Error for RestError
impl Error for RestError
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
Source§impl IntoResponse for RestError
impl IntoResponse for RestError
Source§fn into_response(self) -> Response
fn into_response(self) -> Response
Auto Trait Implementations§
impl Freeze for RestError
impl !RefUnwindSafe for RestError
impl Send for RestError
impl Sync for RestError
impl Unpin for RestError
impl !UnwindSafe for RestError
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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> 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