Flask restful - Exception handling traceback?
<p>I am using Flask Restful to build an API. I have a number of model classes with methods that may raise custom exceptions (for example: AuthFailed exception on my User model class). I am using the custom error handling, documented <a href="http://flask-restful-cn.readthedocs.io/en/0.3.4/extending.html#custom-error-handlers" rel="nofollow">here</a>, to handle this (so that when auth fails, an appropriate response is sent). So far so good. However, I notice that when the exception is raised although the correct response JSON and status is sent back, I still get a traceback which is not ideal. Usually, if I handle an error (outside of flask) with a try-except block, the except can catch the error and handle it (preventing the traceback). So what is the correct approach here? Am I misunderstanding how to use the errors feature?</p>

<p>Unfortunately for you, it is handled this way "by design" of the Flask-RESTful APIs <code>errors</code> functionality.
The exceptions which are thrown are logged and the corresponding response defined in the <code>errors</code> dict is returned.</p>

<p>However, you can change the level of log output by modifying the log level of Flask's logger like this:</p>

<pre><code>app = Flask(__name__)
app.logger.setLevel(logging.CRITICAL)
</code></pre>

<p>I think you would actually have to set it to <code>CRITICAL</code> because these errors are still getting logged even on log level <code>ERROR</code> as far as I know.</p>

<p>Furthermore, both Flask and Flask-RESTful are open-source. That being said, after looking at the code I found <a href="https://github.com/pallets/flask/blob/0.11.1/flask/app.py#L1576" rel="nofollow">the function of a Flask app that is responsible for adding the exception traceback to the log</a> (Flask version 0.11.1).
Of course you could just create your own <code>App</code> class (extending the original class of Flask) which overrides this method (or a caller of it) and does something else instead. However, you should be careful when updating your Flask version if you make use of undocumented stuff like this.</p>

python flask flask-restful