7

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

1
  • 4
    I'm not quite sure what you need... Please try to clarify your answer so your problem can be better understood. What do you need? What have you tried? Commented Dec 14, 2011 at 11:08

1 Answer 1

30

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
Sign up to request clarification or add additional context in comments.

1 Comment

Clear and helpful, highlighted the fact that jsonify returns a response object, which I had forgotten. It also seems to cover any possible interpretation of the asker's question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.