logo

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

Formats the value using the given formatter. Read more

Formats the value using the given formatter. Read more

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Performs the conversion.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Attaches the provided Context to this type, returning a WithContext wrapper. Read more

Attaches the current Context to this type, returning a WithContext wrapper. Read more

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

Should always be Self

Converts the given value to a String. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

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