HTTPException¶
The HTTPException
class extends the HTTPError
class, the main idea of it, is to handle posible erros and properly reply with the corresponding
HTTP status code.
HTTPException(status, title=None, description=None, headers=None, code=None, display=False, log=True)
Note
Description argument must be a dictionary containing HTTP header fields
Redirect example¶
1 2 3 4 5 6 7 8 9 10 11 12 13 | from zunzuncito import tools
class APIResource(object):
def dispatch(self, request, response):
try:
name = request.path[0]
except:
raise tools.HTTPException(
302,
headers={'Location': '/home'},
log=False}
|
Bad request example¶
1 2 3 4 5 6 7 8 9 10 | from zunzuncito import tools
class APIResource(object):
def dispatch(self, request, response):
try:
name = request.path[0]
except:
raise tools.HTTPException(400)
|
the line:
raise tools.HTTPException(400)
For a request like:
curl -i http://api.zunzun.io/exception
Will reply with this:
1 2 3 4 5 6 7 8 | HTTP/1.1 400 Bad Request
Request-ID:
52c597a700ff0229fef9f477280001737e7a756e7a756e6369746f2d617069000131000100
Content-Type: application/json; charset=UTF-8
Date: Thu, 02 Jan 2014 16:45:27 GMT
Server: Google Frontend
Content-Length: 0
Alternate-Protocol: 80:quic,80:quic
|
This is because the request URI is missing the path and should be something like:
curl -i http://api.zunzun.io/exception/foo
That will return something like:
1 2 3 4 5 6 7 8 9 10 11 12 | HTTP/1.1 200 OK
Request-ID:
52c597d200ff0d89f81dcec4280001737e7a756e7a756e6369746f2d617069000131000100
Content-Type: application/json; charset=UTF-8
Vary: Accept-Encoding
Date: Thu, 02 Jan 2014 16:46:11 GMT
Server: Google Frontend
Cache-Control: private
Alternate-Protocol: 80:quic,80:quic
Transfer-Encoding: chunked
my_api.default.v0.zun_exception.zun_exception
|
Note
If you only pass the integer HTTP status code to the HTTPExecption, only the response headers will be sent.
Body response¶
Besides only replying with the headers you may want to give a more informative / verbose message, the HTTPExeption accept the following arguments:
HTTPException(status, title=None, description=None, headers=None, code=None, display=False, log=True)
For example the following snippet of code taken from zun_exception.py:
1 2 3 4 5 6 7 | if name != 'foo':
raise tools.HTTPException(
406,
title='exeption example',
description='name must be foo',
code='my-custom-code',
display=True)
|
When the request is:
curl -i http://api.zunzun.io/v0/exception/naranjas
Notice that the path in this case is:
path = ['naranjas']
Will reply with something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | HTTP/1.1 406 Not Acceptable
Request-ID: 52c59bdf00ff0b7042cbfd5d120001737e7a756e7a756e6369746f2d617069000131000100
Content-Type: application/json; charset=UTF-8
Vary: Accept-Encoding
Date: Thu, 02 Jan 2014 17:03:27 GMT
Server: Google Frontend
Cache-Control: private
Alternate-Protocol: 80:quic,80:quic
Transfer-Encoding: chunked
{
"code": "my-custom-code",
"description": "name must be foo",
"status": "406",
"title": "exeption example"
}
|
A great amount of time has been spent creating, crafting and maintaining this software, please consider donating.
Donating helps ensure continued support, development and availability.
comments powered by Disqus