Flask-Restful taking over exception handling from Flask during non debug mode

2024/10/6 4:06:38

I've used Flask's exception handling during development (@app.errorhander(MyException)) which worked fine even for exceptions coming from Flask-Restful endpoints.

However, I noticed that when switching to debug=False, Flask-Restful is taking over the exception handling entirely (as with this propagate_exceptions is False too). I like that Flask-Restful is sending internal server errors for all unhandled exceptions, but unfortunately this also happens for those that have a Flask exception handler (when these exceptions are coming from a Flask-Restful endpoint).

Is there a way to tell Flask-Restful to only handle exceptions that the Flask error handler wouldn't handle? If not, can I exclude certain exception types from being handled by Flask-Restful, so they get handled by Flask?

My last option is to override Flask-Restful's Api.handle_error and implement this logic myself, but I'd like to use existing APIs first...

Answer

In short my solution just is to create a sub-class of Api that modifies it to only handle exceptions of type HTTPException.

from flask_restful import Api as _Api
from werkzeug.exceptions import HTTPExceptionclass Api(_Api):def error_router(self, original_handler, e):""" Override original error_router to only handle HTTPExceptions. """if self._has_fr_route() and isinstance(e, HTTPException):try:return self.handle_error(e)except Exception:pass  # Fall through to original handlerreturn original_handler(e)

That said, I think overriding app.handle_user_exception and app.handle_exception is a bad design decision in the first place for several reasons.

https://en.xdnf.cn/q/70408.html

Related Q&A

Fetching data with snowflake connector throws EmptyPyArrowIterator error

I use python snowflake connector in my python script (plotly dash app) and today the app stopped working without me changing the code. I tried a couple of things to find out what might be the issue and…

What does epochs mean in Doc2Vec and train when I have to manually run the iteration?

I am trying to understand the epochs parameter in the Doc2Vec function and epochs parameter in the train function. In the following code snippet, I manually set up a loop of 4000 iterations. Is it requ…

TensorFlow 2.0 How to get trainable variables from tf.keras.layers layers, like Conv2D or Dense

I have been trying to get the trainable variables from my layers and cant figure out a way to make it work. So here is what I have tried:I have tried accessing the kernel and bias attribute of the Dens…

Convert Excel row,column indices to alphanumeric cell reference in python/openpyxl

I want to convert the row and column indices into an Excel alphanumeric cell reference like A1. Im using python and openpyxl, and I suspect theres a utility somewhere in that package that does this, bu…

Flask-admin - how to change formatting of columns - get URLs to display

Question on flask-admin. I setup flask-admin and one of the models i created is pulling urls and url titles from a mysql database. Using flask-admin, how to i get flask-admin to render the urls instea…

Stream audio from pyaudio with Flask to HTML5

I want to stream the audio of my microphone (that is being recorded via pyaudio) via Flask to any client that connects.This is where the audio comes from:def getSound(self):# Current chunk of audio dat…

Adding into Path var while silent installation of Python - possible bug?

I need to passively install Python in my applications package installation so i use the following:python-3.5.4-amd64.exe /passive PrependPath=1according this: 3.1.4. Installing Without UI I use the Pre…

Pandas add new columns based on splitting another column

I have a pandas dataframe like the following:A B US,65,AMAZON 2016 US,65,EBAY 2016My goal is to get to look like this:A B country code com US.65.AMAZON 2016…

Proper way to insert HTML into Flask

I know how to send some plain text from Python with render_template when a URL is visited:@app.route(/success.html") ... return render_template("index.html", text="This text goes in…

How to add a new class to an existing classifier in deep learning?

I trained a deep learning model to classify the given images into three classes. Now I want to add one more class to my model. I tried to check out "Online learning", but it seems to train on…