Struct poem::error::Error [−][src]
pub struct Error { /* fields omitted */ }
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, 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> {
todo!()
}
#[handler]
async fn index() -> Result<()> {
Ok(do_something()?)
}
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
Create a new error object from any error type with 503 INTERNAL_SERVER_ERROR
status code.
create a new error object from status code.
Create a new error object from string with 503 INTERNAL_SERVER_ERROR
status code.
Returns the status code of this error.
Downcast this error object by reference.
Attempts to downcast the error to a concrete error type.
Returns true
if the error type is the same as T
.
Consumes this to return a response object.
Trait Implementations
Performs the conversion.
Performs the conversion.
Auto Trait Implementations
impl !RefUnwindSafe for Error
impl !UnwindSafe for Error
Blanket Implementations
Mutably borrows from an owned value. Read more
pub fn vzip(self) -> V
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more