pecan/scaffolds/rest-api/+package+/errors.py
raw| 1 | import json |
| 2 | import webob |
| 3 | from pecan.hooks import PecanHook |
| 4 | |
| 5 | |
| 6 | class JSONErrorHook(PecanHook): |
| 7 | """ |
| 8 | A pecan hook that translates webob HTTP errors into a JSON format. |
| 9 | """ |
| 10 | |
| 11 | def on_error(self, state, exc): |
| 12 | if isinstance(exc, webob.exc.HTTPError): |
| 13 | return webob.Response( |
| 14 | body=json.dumps({'reason': str(exc)}), |
| 15 | status=exc.status, |
| 16 | headerlist=exc.headerlist, |
| 17 | content_type='application/json' |
| 18 | ) |