pub struct Error { /* private fields */ }
Expand description
General response error.
Create from any error types
use poem::{error::InternalServerError, handler, Result};
#[handler]
async fn index() -> Result<String> {
Ok(std::fs::read_to_string("example.txt").map_err(InternalServerError)?)
}
Create you own error type
use poem::{error::ResponseError, handler, http::StatusCode, Endpoint, Request, Result};
#[derive(Debug, thiserror::Error)]
#[error("my error")]
struct MyError;
impl ResponseError for MyError {
fn status(&self) -> StatusCode {
StatusCode::BAD_GATEWAY
}
}
fn do_something() -> Result<(), MyError> {
Err(MyError)
}
#[handler]
async fn index() -> Result<()> {
Ok(do_something()?)
}
let resp = index.get_response(Request::default()).await;
assert_eq!(resp.status(), StatusCode::BAD_GATEWAY);
assert_eq!(resp.into_body().into_string().await.unwrap(), "my error");
Custom error response
use poem::{error::ResponseError, handler, http::StatusCode, Response, Result, Request, Body, Endpoint};
#[derive(Debug, thiserror::Error)]
#[error("my error")]
struct MyError;
impl ResponseError for MyError {
fn status(&self) -> StatusCode {
StatusCode::BAD_GATEWAY
}
fn as_response(&self) -> Response {
let body = Body::from_json(serde_json::json!({
"code": 1000,
"message": self.to_string(),
})).unwrap();
Response::builder().status(self.status()).body(body)
}
}
fn do_something() -> Result<(), MyError> {
Err(MyError)
}
#[handler]
async fn index() -> Result<()> {
Ok(do_something()?)
}
let resp = index.get_response(Request::default()).await;
assert_eq!(resp.status(), StatusCode::BAD_GATEWAY);
assert_eq!(resp.into_body().into_json::<serde_json::Value>().await.unwrap(),
serde_json::json!({
"code": 1000,
"message": "my error",
}));
Downcast the error to concrete error type
use poem::{error::NotFoundError, Error};
let err: Error = NotFoundError.into();
assert!(err.is::<NotFoundError>());
assert_eq!(err.downcast_ref::<NotFoundError>(), Some(&NotFoundError));
Implementations
sourceimpl Error
impl Error
sourcepub fn new<T: StdError + Send + Sync + 'static>(
err: T,
status: StatusCode
) -> Self
pub fn new<T: StdError + Send + Sync + 'static>(
err: T,
status: StatusCode
) -> Self
Create a new error object from any error type with a status code.
sourcepub fn from_status(status: StatusCode) -> Self
pub fn from_status(status: StatusCode) -> Self
create a new error object from status code.
sourcepub fn from_string(msg: impl Into<String>, status: StatusCode) -> Self
pub fn from_string(msg: impl Into<String>, status: StatusCode) -> Self
Create a new error object from a string with a status code.
sourcepub fn downcast_ref<T: StdError + Send + Sync + 'static>(&self) -> Option<&T>
pub fn downcast_ref<T: StdError + Send + Sync + 'static>(&self) -> Option<&T>
Downcast this error object by reference.
sourcepub fn downcast<T: StdError + Send + Sync + 'static>(self) -> Result<T, Error>
pub fn downcast<T: StdError + Send + Sync + 'static>(self) -> Result<T, Error>
Attempts to downcast the error to a concrete error type.
sourcepub fn is<T: StdError + Debug + Send + Sync + 'static>(&self) -> bool
pub fn is<T: StdError + Debug + Send + Sync + 'static>(&self) -> bool
Returns true
if the error type is the same as T
.
sourcepub fn as_response(&self) -> Response
pub fn as_response(&self) -> Response
Consumes this to return a response object.
Trait Implementations
sourceimpl From<Infallible> for Error
impl From<Infallible> for Error
sourcefn from(_: Infallible) -> Self
fn from(_: Infallible) -> Self
Performs the conversion.
sourceimpl From<StatusCode> for Error
impl From<StatusCode> for Error
sourcefn from(status: StatusCode) -> Self
fn from(status: StatusCode) -> Self
Performs the conversion.
Auto Trait Implementations
impl !RefUnwindSafe for Error
impl Send for Error
impl Sync for Error
impl Unpin for Error
impl !UnwindSafe for Error
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> FutureExt for T
impl<T> FutureExt for T
sourcefn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
sourcefn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
fn vzip(self) -> V
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more