Question : flask error handling
Answered by : ugly-unicorn-lu2jg3dl8cfp
from flask import json
from werkzeug.exceptions import HTTPException
@app.errorhandler(HTTPException)
def handle_exception(e): """Return JSON instead of HTML for HTTP errors.""" # start with the correct headers and status code from the error response = e.get_response() # replace the body with JSON response.data = json.dumps({ "code": e.code, "name": e.name, "description": e.description, }) response.content_type = "application/json" return response
Source : https://flask.palletsprojects.com/en/2.1.x/errorhandling/ | Last Update : Wed, 01 Jun 22
Question : flask error handling
Answered by : ugly-unicorn-lu2jg3dl8cfp
from werkzeug.exceptions import HTTPException
@app.errorhandler(Exception)
def handle_exception(e): # pass through HTTP errors if isinstance(e, HTTPException): return e # now you're handling non-HTTP exceptions only return render_template("500_generic.html", e=e), 500
Source : https://flask.palletsprojects.com/en/2.1.x/errorhandling/ | Last Update : Wed, 01 Jun 22