#http #api #json #error #7807

http-api-problem

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

35 releases (14 breaking)

✓ Uses Rust 2018 edition

0.17.0 Feb 21, 2020
0.15.0 Jan 30, 2020
0.13.1 Aug 1, 2019
0.13.0 Jun 5, 2019
0.1.2 Jul 11, 2017

#31 in Web programming

Download history 504/week @ 2020-02-08 814/week @ 2020-02-15 754/week @ 2020-02-22 241/week @ 2020-02-29 160/week @ 2020-03-07 57/week @ 2020-03-14 179/week @ 2020-03-21 246/week @ 2020-03-28 384/week @ 2020-04-04 468/week @ 2020-04-11 366/week @ 2020-04-18 457/week @ 2020-04-25 787/week @ 2020-05-02 912/week @ 2020-05-09 501/week @ 2020-05-16 389/week @ 2020-05-23

1,362 downloads per month
Used in nakadion

Apache-2.0/MIT and maybe MPL-2.0

34KB
542 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::with_title_and_type_from_status(HttpStatusCode::NotFound)
    .set_detail("detailed explanation")
    .set_instance("/on/1234/do/something");

assert_eq!(Some("https://httpstatuses.com/404".to_string()), p.type_url);
assert_eq!(Some(HttpStatusCode::NotFound), p.status);
assert_eq!("Not Found".to_string(), p.title);
assert_eq!(Some("detailed explanation".to_string()), p.detail);
assert_eq!(Some("/on/1234/do/something".to_string()), p.instance);

There is also From<u16> implemented for HttpStatusCode:

use http_api_problem::*;

let p = HttpApiProblem::with_title_and_type_from_status(428)
    .set_detail("detailed explanation")
    .set_instance("/on/1234/do/something");

assert_eq!(Some("https://httpstatuses.com/428".to_string()), p.type_url);
assert_eq!(Some(HttpStatusCode::PreconditionRequired), p.status);
assert_eq!("Precondition Required".to_string(), p.title);
assert_eq!(Some("detailed explanation".to_string()), p.detail);
assert_eq!(Some("/on/1234/do/something".to_string()), p.instance);

Features

Web Frameworks

There are multiple features to integrate with web frameworks:

  • with-warp
  • with-hyper
  • with-actix-web

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

ApiError

The feature with-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

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.4–5MB
~115K SLoC