Skip to main content

HttpError

Trait HttpError 

Source
pub trait HttpError:
    Error
    + Send
    + Sync
    + 'static {
    // Provided methods
    fn status_code(&self) -> u16 { ... }
    fn error_message(&self) -> String { ... }
}
Expand description

Trait for errors that can be converted to HTTP responses

Implement this trait on your domain errors to customize the HTTP status code and message that will be returned when the error is converted to a response.

§Example

use ferro_rs::HttpError;

#[derive(Debug)]
struct UserNotFoundError { user_id: i32 }

impl std::fmt::Display for UserNotFoundError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "User {} not found", self.user_id)
    }
}

impl std::error::Error for UserNotFoundError {}

impl HttpError for UserNotFoundError {
    fn status_code(&self) -> u16 { 404 }
}

Provided Methods§

Source

fn status_code(&self) -> u16

HTTP status code (default: 500)

Source

fn error_message(&self) -> String

Error message for HTTP response (default: error’s Display)

Implementors§