Browse Source

flaskr: report app init error instead of crashing

Improve error reporting of the application factory built during the tutorial
by not swallowing all OSError exceptions that could happen when trying to
create the instance folder.
pull/2930/head
Nechepso 6 years ago
parent
commit
0639bfaf23
  1. 6
      docs/tutorial/factory.rst
  2. 6
      examples/tutorial/flaskr/__init__.py

6
docs/tutorial/factory.rst

@ -35,6 +35,7 @@ directory should be treated as a package.
.. code-block:: python .. code-block:: python
:caption: ``flaskr/__init__.py`` :caption: ``flaskr/__init__.py``
import errno
import os import os
from flask import Flask from flask import Flask
@ -58,8 +59,9 @@ directory should be treated as a package.
# ensure the instance folder exists # ensure the instance folder exists
try: try:
os.makedirs(app.instance_path) os.makedirs(app.instance_path)
except OSError: except OSError as e:
pass if e.errno != errno.EEXIST:
raise
# a simple page that says hello # a simple page that says hello
@app.route('/hello') @app.route('/hello')

6
examples/tutorial/flaskr/__init__.py

@ -1,3 +1,4 @@
import errno
import os import os
from flask import Flask from flask import Flask
@ -23,8 +24,9 @@ def create_app(test_config=None):
# ensure the instance folder exists # ensure the instance folder exists
try: try:
os.makedirs(app.instance_path) os.makedirs(app.instance_path)
except OSError: except OSError as e:
pass if e.errno != errno.EEXIST:
raise
@app.route('/hello') @app.route('/hello')
def hello(): def hello():

Loading…
Cancel
Save