Keep simpleserver active even with syntax errors

Is there a way to set up a simple server that Flask uses to not exit with every syntax error?

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

I am currently using this setting for a simple server. Setting passthrough_errors to False means that most errors actually support the process, so I can use the interactive debugger, however, syntax errors still exit the program. I tried different configuration values, but I did not find anything. Thanks!

+4
source share
2 answers

According to the Python documentation, there are two types or errors:

  • Syntax errors
  • Exceptions

Syntax errors occur during the parsing time (at the moment your code does not execute , so you have no way to catch errors, because the syntax time is not the execution time when your code actually runs).

The only way to catch syntax errors is when they occur inside a piece of code specified as an argument to the exec function (a python code line is executed):

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

But you should remember that you use exec () only when you really know what you are doing. It is not recommended to use exec () at all, especially if it depends on user input.

+3
source

I just posted the Flask-Failsafe extension to solve this problem.

I clicked on it all the time and earlier I met with your message about finding a solution. After several experiments, I hacked into a decorator that you can use to wrap your initialization code so that in case of a failure the loader continues to work. Check it out and let me know what you think.

+5
source

All Articles