Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask JSON Custom Error Page

Tags:

python

flask

is there any implementation exists on JSON as custom error page on Flask?

like image 404
herlambang Avatar asked Dec 14 '11 10:12

herlambang


1 Answers

You can create a json response object using the "jsonify" helper from flask and then set the status_code of the response before returning it like this:

def not_found(error):
    response = jsonify({'code': 404,'message': 'No interface defined for URL'})
    response.status_code = 404
    return response

You can register this function as the handler by wrapping it in the errorhandler:

@app.errorhandler(404)
def not_found(error):
    ...

OR, setting it directly on the error_handler_spec:

app.error_handler_spec[None][404] = not_found
like image 72
rootsmith Avatar answered Oct 19 '22 16:10

rootsmith