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-0.0.5.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 469681ec542d6a4200bf8d9e27400f517b5bcd41a2d11b9fa3c0dd585486022a |
|
MD5 | 958e43e2fe3bc16fe518327e1c20d0be |
|
BLAKE2b-256 | 99e1fd1e25b06dc76470de45690219a220d5a590a58aaed44e617f2bfd5c7dd6 |
Hashes for tornado_problem_details-0.0.5-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e1c03ce10bf348bcb2263228a9cbbb63c1304c46a562acbefdfc77b5dd65209d |
|
MD5 | ea7fd0a32d60bb1da1b6d69b95ee50dd |
|
BLAKE2b-256 | 428ca7a17150ec5b4d2f09df5b6c86c277d97bfcfe5d6089448b93b7ac2f3326 |