RFC-7807 Error Documents for Tornado
Project description
This library provides a version of tornado.web.RequestHandler.send_error that speaks application/problem+json instead of HTML. The easiest way to use this library is to inherit from problemdetails.ErrorWriter and raise problemdetails.Problem exceptions instead of HTTPError.
class MyHandler(problemdetails.ErrorWriter, web.RequestHandler):
def get(self):
if not self.do_something_hard():
raise problemdetails.Problem(status_code=500,
title='Failed to do_something_hard')
HTTP/1.1 500 Internal Server Error
Content-Type: application/problem+json
{
"status": 500,
"title": "Failed to do_something_hard",
"type": "https://tools.ietf.org/html/rfc7231#section-6.6.1"
}
You can easily construct more substantial response documents by passing additional keyword parameters to the problemdetails.Problem initializer. They become top-level properties in the response document.
You can also call send_error directly and produce a response docuemnt. The following snippet produces the same output as the previous snippet.
class MyHandler(problemdetails.ErrorWriter, web.RequestHandler):
def get(self):
try:
self.do_something_hard()
except SomeException as error:
self.send_error(500, title="Failed to do_something_hard")
The interface of tornado.web.RequestHandler.send_error is less expressive since keyword parameters may be swallowed by intervening code. The only parameters that are recognized are: instance, title, and type. Use the exception-based interface for more substantial documents.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for tornado-problem-details-1.0.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2dbbd6fcea885531dce55263a6e5e5ba3bf5d67b5e9ec8828075df41d3c80cfb |
|
MD5 | f70afe63ca0d104eb5b45a7a948d73c3 |
|
BLAKE2b-256 | 6c1c3fd5e619250a9b941ef35fe16bb7778cb14ffc00086ac5af65ad56a76b32 |
Hashes for tornado_problem_details-1.0.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e77fabe816f288073fa7ad32f0131ee13f653f804ea723492865be7883616801 |
|
MD5 | 6e96ea27d57058c8dcf61450a9175795 |
|
BLAKE2b-256 | d6ee2800ad00f93cbb3cb78de1c856a18dddd4a6d0b68b415dea886e2828d94b |