How to prevent Flask (python) from emitting html?

Solution
@app.errorhandler(HTTPException)
def handle_exception(exception):
    response = exception.get_response()
    response.content_type = "application/json"
    response.data = json.dumps({"code": exception.code})
    return response