okfn / timemapper

Create and share elegant timelines and timemaps fast
http://timemapper.okfnlabs.org/
MIT License
274 stars 60 forks source link

Auto convert exceptions to json where json expected #10

Closed rufuspollock closed 12 years ago

rufuspollock commented 13 years ago

Borrow from excellent work of pudo in the datahub (see web.py):

@app.errorhandler(401)
@app.errorhandler(403)
@app.errorhandler(404)
@app.errorhandler(410)
@app.errorhandler(500)
def handle_exceptions(exc):
    """ Re-format exceptions to JSON if accept requires that. """
    format = response_format(app, request)
    if format == 'json':
        body = {'status': exc.code,
                'name': exc.name,
                'description': exc.get_description(request.environ)}
        return jsonify(body, status=exc.code,
                       headers=exc.get_headers(request.environ))
    return exc
rufuspollock commented 12 years ago

This was for python so no longer valid.