[][src]Function routerify_json_response::json_failed_resp_with_message

pub fn json_failed_resp_with_message<B, M>(
    code: StatusCode,
    message: M
) -> Result<Response<B>> where
    B: HttpBody + From<Vec<u8>> + Send + Sync + Unpin + 'static,
    M: Into<String>, 

Generates a failed JSON response with the provided message and status code.

It generates JSON response in the following JSON format:

{
    "status": "failed",
    "code": "<status_code>",
    "message": "<error_message>"
}

Examples

use hyper::{Body, Request, Response, StatusCode};
use routerify_json_response::{json_failed_resp_with_message};

async fn list_books_handler(_: Request<Body>) -> Result<Response<Body>, routerify::Error> {
    // Generate a failed JSON response in the following format:
    // { "status": "failed", code: 500, data: "Internal Server Error: Couldn't fetch book list from database" }
    json_failed_resp_with_message(
        StatusCode::INTERNAL_SERVER_ERROR,
        "Couldn't fetch book list from database",
     )
}