librelist archives

« back to archive

Catching exceptions from Flask

Catching exceptions from Flask

From:
Brandon Stafford
Date:
2012-01-27 @ 20:40
Hi all,

I've been trying to catch exceptions from Flask when it's running on uWSGI.
(I know I can use the debugger when running on the development server, but
I'd like to be able to catch exceptions in our production configuration,
though not actually running live.)

With a basic WSGI app, if I start it like this:

uwsgi --http :9090 --master --catch-exceptions --wsgi-file hello.py

I get responses like this:

HTTP/1.1 500 Internal Server Error Content-type: text/plain Traceback (most
recent call last): File "hello.py", line 3, in application aksjhdkjasdhjas
NameError: global name 'aksjhdkjasdhjas' is not defined

But with a similar basic Flask app, the exception does gets swallowed up
somewhere, and I just get a standard 500 error response. Is there some way
to make Flask pass the exceptions to uWSGI?

Here's the basic WSGI app I was using: http://pastebin.com/GL6zeUfH
Here's my Flask app with the same syntax error induced:
http://pastebin.com/1XDaP4q9

Thanks,
Brandon

--
Brandon Stafford
Rascal Micro: small computers for art and science
Somerville, MA, USA

Re: [flask] Catching exceptions from Flask

From:
Simon Sapin
Date:
2012-01-27 @ 20:44
Le 27/01/2012 21:40, Brandon Stafford a écrit :
> But with a similar basic Flask app, the exception does gets swallowed up
> somewhere, and I just get a standard 500 error response. Is there some
> way to make Flask pass the exceptions to uWSGI?

The exception is caught by Flask. You want to set

app.config['PROPAGATE_EXCEPTIONS'] = True

See http://flask.pocoo.org/docs/config/#builtin-configuration-values

Regards,
-- 
Simon Sapin

Re: [flask] Catching exceptions from Flask

From:
Brandon Stafford
Date:
2012-01-27 @ 20:58
Thanks for the quick response, Simon. That works great!

Just for the sake of future searchers, here's the test that passed a
SyntaxError exception to uWSGI:

from flask import Flask
application = Flask(__name__)
application.config['PROPAGATE_EXCEPTIONS'] = True

@application.route('/')
def hello_world():
    aksjhdkjasdhjas
    return 'Hello World!'

Thanks,
Brandon

On Fri, Jan 27, 2012 at 3:44 PM, Simon Sapin <simon.sapin@exyr.org> wrote:

> Le 27/01/2012 21:40, Brandon Stafford a écrit :
> > But with a similar basic Flask app, the exception does gets swallowed up
> > somewhere, and I just get a standard 500 error response. Is there some
> > way to make Flask pass the exceptions to uWSGI?
>
> The exception is caught by Flask. You want to set
>
> app.config['PROPAGATE_EXCEPTIONS'] = True
>
> See http://flask.pocoo.org/docs/config/#builtin-configuration-values
>
> Regards,
> --
> Simon Sapin
>



-- 
Brandon Stafford
Rascal Micro: small computers for art and science
Somerville, MA, USA