Search code examples
pythonflaskwsgiwerkzeug

Keep simpleserver active even on syntax errors


Is there a way to configure the simple-server that Flask uses to not exit on every single syntax error?

app = Flask(__name__)
app.run(host='0.0.0.0', debug=True, use_debugger=True, passthrough_errors=False);

Currently I'm using this setup for the simple-server. Setting passthrough_errors to False means most of the errors actually keeps the process alive so that I can use the interactive debugger, syntax errors still exits the program though. I've tried different configuration values but I have not found anything that works. Thanks!


Solution

  • According to Python documentation there are two types or errors:

    1. Syntax Errors
    2. Exceptions

    Syntax errors are produced during parse time (at that moment your code doesn't actually executes, so you have no possibility to catch errors, since parse time is not a runtime, when your code actually executes).

    The only way you can catch syntax errors is when they happen inside a piece of code given as an argument to exec function (executes string of python code):

    >>> try:
    ...     exec('x===6')
    ... except SyntaxError:
    ...     print('Hello!')
    ...
    Hello!
    

    But you must remember to use exec() only when you really know what you do. It's not recommended to use exec() at all especially when it depends on user input.