#http-api #http-error #error-response #api-error #error #http-response #http

http-api-problem

A library to create HTTP error response content for APIs based on RFC 7807

52 releases (29 breaking)

0.58.0 Dec 18, 2023
0.57.0 Jul 4, 2023
0.56.0 Nov 25, 2022
0.53.0 Jun 28, 2022
0.1.2 Jul 11, 2017

#134 in Web programming

Download history 2330/week @ 2024-01-01 2530/week @ 2024-01-08 2305/week @ 2024-01-15 2794/week @ 2024-01-22 2251/week @ 2024-01-29 2055/week @ 2024-02-05 1696/week @ 2024-02-12 1728/week @ 2024-02-19 1491/week @ 2024-02-26 1607/week @ 2024-03-04 2056/week @ 2024-03-11 2448/week @ 2024-03-18 2051/week @ 2024-03-25 2535/week @ 2024-04-01 1952/week @ 2024-04-08 1478/week @ 2024-04-15

8,054 downloads per month
Used in 20 crates (8 directly)

Apache-2.0/MIT

66KB
997 lines

HTTP-API-PROBLEM

crates.io docs.rs downloads CI license-mit license-apache

A library to create HTTP response content for APIs based on RFC7807.

Usage

Get the latest version for your Cargo.toml from crates.io.

Serde

HttpApiProblem implements Serialize and Deserialize.

Examples

use http_api_problem::*;
let p = HttpApiProblem::new(StatusCode::UNPROCESSABLE_ENTITY)
    .title("You do not have enough credit.")
    .detail("Your current balance is 30, but that costs 50.")
    .type_url("https://example.com/probs/out-of-credit")
    .instance("/account/12345/msgs/abc");

assert_eq!(Some(StatusCode::UNPROCESSABLE_ENTITY), p.status);
assert_eq!(Some("You do not have enough credit."), p.title.as_deref());
assert_eq!(Some("Your current balance is 30, but that costs 50."), p.detail.as_deref());
assert_eq!(Some("https://example.com/probs/out-of-credit"), p.type_url.as_deref());
assert_eq!(Some("/account/12345/msgs/abc"), p.instance.as_deref());

There is also TryFrom<u16> implemented for [StatusCode]:

use http_api_problem::*;
let p = HttpApiProblem::try_new(422).unwrap()
    .title("You do not have enough credit.")
    .detail("Your current balance is 30, but that costs 50.")
    .type_url("https://example.com/probs/out-of-credit")
    .instance("/account/12345/msgs/abc");

assert_eq!(Some(StatusCode::UNPROCESSABLE_ENTITY), p.status);
assert_eq!(Some("You do not have enough credit."), p.title.as_deref());
assert_eq!(Some("Your current balance is 30, but that costs 50."), p.detail.as_deref());
assert_eq!(Some("https://example.com/probs/out-of-credit"), p.type_url.as_deref());
assert_eq!(Some("/account/12345/msgs/abc"), p.instance.as_deref());

Features

Web Frameworks

There are multiple features to integrate with web frameworks:

  • axum
  • warp
  • hyper
  • actix-web
  • salvo
  • tide
  • rocket

These mainly convert the HttpApiProblem to response types of the frameworks and implement traits to integrate with the frameworks error handling

ApiError

The feature api-error enables a structure which can be return from "api handlers" that generate responses and can be converted into an HttpApiProblem.

Thank you

A big "thank you" for contributions and inspirations goes to the following GitHub users:

  • panicbit
  • thomaseizinger
  • SohumB

License

http-api-problem is primarily distributed under the terms of both the MIT license and the Apache License (Version 2.0).

Copyright (c) 2017 Christian Douven.

License: Apache-2.0/MIT

Dependencies

~1–39MB
~587K SLoC