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 call send_error() with additional parameters.
from tornado import web
import problemdetails
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")
HTTP/1.1 500 Internal Server Error
Content-Type: application/problem+json
{
"title": "Failed to do_something_hard",
"type": "https://tools.ietf.org/html/rfc7231#section-6.6.1"
}
As an alternative, you can raise a problemdetails.Problem instance and let the Tornado exception handling take care of eventually calling write_error. The following snippet produces the same output as the previous example.
from tornado import web
import problemdetails
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')
The problemdetails.Problem instance passes all of the keyword parameters through in the resulting message so it is very easy to add fields. The interface of tornado.web.RequestHandler.send_error is less expressive since keyword parameters may be swallowed by intervening code.
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.3.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | b8de1fb854a7902abf50a2cb7263d24b7a4bf0d5cf3a00f9bce77d93f9ff82f6 |
|
MD5 | 24f0df3e5875a83724b0e8cc7be411b1 |
|
BLAKE2b-256 | 72828450e0bfaf0baea69ae7c2673a31b15c2a156300eb3ebfb99f59adc76ae2 |
Hashes for tornado_problem_details-0.0.3-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 10ff88038d146d27267180a8a7a9ae87d52279907e88a96078b1640f79f1fb7a |
|
MD5 | 1f7c4a2db4331410915de53459638b6f |
|
BLAKE2b-256 | c8ca5eecc4db1f8f875e0a67088554459ba9bd2629e2f7a2bae60ed56466a999 |