Skip to main content

HttpError

Trait HttpError 

Source
pub trait HttpError: Debug {
    // Required methods
    fn status_code(&self) -> u16;
    fn error_code(&self) -> ErrorCode;
    fn detail(&self) -> String;
}
Expand description

Implement this trait on domain error types to get a blanket From implementation into ApiError for free.

§Examples

use api_bones::error::{ApiError, ErrorCode, HttpError};

#[derive(Debug)]
struct BookingNotFound(u64);

impl HttpError for BookingNotFound {
    fn status_code(&self) -> u16 { 404 }
    fn error_code(&self) -> ErrorCode { ErrorCode::ResourceNotFound }
    fn detail(&self) -> String { format!("Booking {} not found", self.0) }
}

let err: ApiError = BookingNotFound(42).into();
assert_eq!(err.status, 404);
assert_eq!(err.detail, "Booking 42 not found");

Required Methods§

Source

fn status_code(&self) -> u16

HTTP status code (e.g. 404).

Source

fn error_code(&self) -> ErrorCode

Machine-readable ErrorCode for this error.

Source

fn detail(&self) -> String

Human-readable detail string (RFC 9457 §3.1.4 detail).

Implementors§