Here is the code:
app.py
from flask import Flask, render_template
app = Flask(__name__)
app.config['DEBUG'] = True
@app.route('/')
def index():
return render_template('unknown')
if __name__ == '__main__':
from werkzeug.serving import run_simple
run_simple('127.0.0.1', 5000, app)
Run it by
python app.py
or
export FLASK_APP=app.py
flask run
then visit http://127.0.0.1:5000 will see ERR_EMPTY_RESPONSE in browser and nothing in console.
you are using werkzueg's simple_run, in this way , app.config.DEBUG will not work, if you want it to work , you should use:
run_simple('127.0.0.1', 5000, app, use_debugger=True)
or
app.run('127.0.0.1', 5000, debug=True)
if you want to use config.DEBUG = True work, you may run as wsgi server.
I expect it print exception traceback in console and show 500 internal server error in browser.
when DEBUG is False, it will print exception traceback and show 500 in browser, but when DEBUG is True, it not, so I think it is a bug.
@guyskk which version Flask does you using?
i checkout at v0.10.1 it print exception trackback in console when DEBUG is True and not print when DEBUG is False
and at v0.11.1 both DEBUG is True or False, it show trackback in console.
@luke0922 flask 0.11.1 and python3.5
@guyskk it seems work as you said, i just try in python 3.5.1 and get the same result, maybe it's a bug.
Can reproduce. No problems with Python 2.
I reproduced this with flask 0.11.1 and python 3.5.2
I found the jinja2.exceptions.TemplateNotFound exception was catch by wrong place
try:
execute(self.server.app)
except (socket.error, socket.timeout) as e: # go here
self.connection_dropped(e, environ)
except Exception:
if self.server.passthrough_errors:
raise
and in python3.5
issubclass(jinja2.exceptions.TemplateNotFound, socket.error)
True
here is the thing:
In [23]: jinja2.exceptions.TemplateNotFound.__bases__
Out[23]: (OSError, LookupError, jinja2.exceptions.TemplateError)
In [24]: socket.error is OSError
Out[24]: True
I see, I think under Python 3 we should use one of the other error types that the socket module has to offer: https://docs.python.org/3/library/socket.html#exceptions -- not sure if herror and gaierror catch everything though.
Also thank you so much @qingyunha for finding this!
Cool stuff. I think we could keep a tuple of error types for socket stuff in the compat module and catch on that.
Not my favorite change.
Apparently this change was done in Python 3.3 too.
BTW, the reason this exception even bubbles up this far is https://github.com/pallets/werkzeug/issues/954
We could try to detect the actual error codes relevant for socket loss.
is this reasonable that Template-Not-Found is an OS error?
I don't really know but changing it won't solve the problem (because there might be other errors that get accidentally caught)
On 28 August 2016 11:58:09 CEST, qingyunha [email protected] wrote:
is this reasonable that Template-Not-Found is an OS error?
You are receiving this because you commented.
Reply to this email directly or view it on GitHub:
https://github.com/pallets/flask/issues/1995#issuecomment-242966274
Sent from my Android device with K-9 Mail. Please excuse my brevity.
We could also detect if the exception is of the exact type socket.error, and not a subclass.
We can probably determine error is not subclass of OSError but socket.error. socket.error inherit not only OSError but also IOError (FYI).
issubclass(socket.error, IOError) # True
issubclass(socket.error, OSError) # True
Yes, but also:
issubclass(OSError, IOError) # True
issubclass(IOError, OSError) # True
So I don't think this will help us much.
>>> # Python 3
>>> socket.error is OSError
True
>>> socket.error()
OSError()
In Python 3, socket.error isn't a subclass, it's just an alias of OSError. In Python 2 it is a distinct type, _and_ not a subclass of OSError.
>>> # Python 2
>>> issubclass(socket.error, OSError)
False
This is an issue with Werkzeug's dev server, not with Flask. Other servers such as Gunicorn do not have the issue. Reported at pallets/werkzeug#1127.
Most helpful comment
I found the
jinja2.exceptions.TemplateNotFoundexception was catch by wrong placeand in python3.5