Home > Enterprise >  In Flask is there a way to handle all the type of errors with one error handler & return the type of
In Flask is there a way to handle all the type of errors with one error handler & return the type of

Time:02-03

Is there anyway to handle all the type of error with one definition & return the type of error occurred , For example in the below code error handeller is there for 404 but if I am unaware of other possible errors how can I print the error on html page

from flask import Flask, abort
app = Flask(__name__)

@app.route("/")
def hello():
    return "Welcome to Python Flask."

@app.errorhandler(404) 
def invalid_route(e): 
    return "Invalid route."

Expected if possible

@app.errorhandler(Type of error) #is there anything I can user here ?
def invalid_route(e): 
    return f"Type of error occurred is {e}"

CodePudding user response:

Yea there are generic exception handlers

from flask import Flask
from werkzeug.exceptions import HTTPException

app = Flask(__name__)


@app.route("/")
def hello():
    return "Welcome to Python Flask."


@app.errorhandler(HTTPException)
def handle_exception(e):
    return f'Type of error occurred is {e.code}'

app.run()

  •  Tags:  
  • Related