axum-internal-server-error 0.1.0

A utility function and trait for mapping error types to internal server errors in axum
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
use std::error::Error;

use axum::http::StatusCode;

pub fn internal_server_error<E: Error>(error: E) -> (StatusCode, String) {
    (StatusCode::INTERNAL_SERVER_ERROR, error.to_string())
}

pub trait InternalServiceError<T> {
    fn internal_server_error(self) -> Result<T, (StatusCode, String)>;
}

impl<T, E: Error> InternalServiceError<T> for Result<T, E> {
    fn internal_server_error(self) -> Result<T, (StatusCode, String)> {
        self.map_err(internal_server_error)
    }
}