Issue
I'm learning how to deploy Flask API app on Azure with Azure Functions.
I have created following wsgi.py file:
from app import create_app
from error_handler import default_error_handler_azure_functions
from flask import jsonify
app = create_app()
@app.errorhandler(Exception)
def handle_error(e):
return default_error_handler_azure_functions(e)
Here's the function_app.py script required by the Azure Functions service:
import azure.functions as func
from flask import Flask, request, Response, redirect, url_for
from wsgi import app
app = func.WsgiFunctionApp(app=app.wsgi_app, http_auth_level=func.AuthLevel.ANONYMOUS)
The app.py script looks like this:
import api
import mongoengine
import flask
from flask_cors import CORS
from error_handler import default_error_handler_azure_functions
def create_app():
app = flask.Flask(__name__)
CORS(app)
app.config.from_prefixed_env()
app.register_blueprint(api.blueprint)
if not "TESTING" in app.config or not app.config["TESTING"]:
mongoengine.connect(host=app.config["MONGO_URI"], name="finapp")
app.register_error_handler(Exception, default_error_handler_azure_functions)
return app
The simple error handler looks like this:
def default_error_handler_azure_functions(e: Exception):
code = 499
ret = {
"Error": "InternalServerError",
"Description": "Unknown error occurred while processing your request.",
"Resolution": "Try again later.",
}
if isinstance(e, Error):
code = e.get_code()
ret = e.get_json()
return flask.jsonify(ret), code
Everything works properly when run in the Azure's App Service or locally with:
app = create_app()
app.run(debug=True, host="0.0.0.0")
When any exception is raised in the application, it is captured by this error handler. However in Azure Functions, exceptions are not captured. It looks like the handler is not called - in logs on Azure I can see the exception is raised and not handled so the server returns 500 Internal Server Error.
My requirements.txt file:
Flask==2.2.2
PyJWT==2.1.0
python-dotenv==0.19.2
pytest==7.1.3
dnspython==1.16.0
flask-restful==0.3.9
mongomock==4.1.2
pytest-cov==4.0.0
black==22.10.0
multipledispatch==0.6.0
mongoengine==0.24.2
iso4217==1.11.20220401
iso18245==1.2.0
pycountry==22.3.5
flask-cors==3.0.10
openapi-core==0.18.2
Werkzeug==2.2.2
aiosmtpd==1.4.4.post2
azure-functions
Am I missing something?
Solution
I have used your code and able to get the expected response when the error_handler
is invoked.
In my case host.json
file looks like below-
{
"version": "2.0",
"logging": {
"applicationInsights": {
"samplingSettings": {
"isEnabled": true,
"excludedTypes": "Request"
}
}
},
"extensionBundle": {
"id": "Microsoft.Azure.Functions.ExtensionBundle",
"version": "[3.*, 4.0.0)"
},
"extensions":
{
"http":
{
"routePrefix": ""
}
}
}
I have deployed my function to function app and got successfully deployed message.
I can see all the related files in the function app.
I am getting the excepted error message along with the error code while triggering the function.
Answered By - Ikhtesam Afrin
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.