[Flask] How to debug exceptions on the production server?

Alejo Arias alejoar at gmail.com
Fri Apr 22 03:37:40 EDT 2016


Hi Gabor,

I'm not sure why your code raises a TypeError, it doesn't seem to be wrong
to me, maybe someone else can help you there.

What I can tell you though is a way to avoid using the try-except at all. I
would use the 'get' method of the dictionary so that trying to access a key
that doesn't exist doesn't raise an exception.

For example:

>>> d = {}
>>> x = d.get('name')
>>> x is None
True

You can also pass a second parameter to default the value to something else
instead of None:

>>> x = d.get('name', 0)
>>> x
0

Hope that helps.

Regards,
Alejo

On 22 April 2016 at 08:06, Gabor Szabo <gabor at szabgab.com> wrote:

> Hi,
>
> I had some code that on the development server running in debug mode
> worked fine, but on the production server gave me an "Internal Error". The
> cause was some difference in data. I assumed a dictionary key to be always
> present, but on the production server some of the data had that key missing.
>
> My real issue however was really that I have not got any indication of the
> nature of the problem.
> The error logs (both uwsgi and nginx) had no information about the error.
> After seeing the "Internal Error" page I wrapped the code in a try-block
> and tried to print the exception. A very simplified version of the code
> that already demonstrate the problem looks like this:
>
> @app.route("/sample")
> def sample():
>     d = {
>     }
>     try:
>         x = d['name']
>     except Exception as e:
>         return(type(e))
>     return 'ok'
>
>
> This code just gives me further "Internal Errors" on the production
> server. Apparently the call to type(e) generates another exception.
>
> In debug mode it gives me a
>
> TypeError: exceptions.KeyError object is not an iterator
>
> error.
>
> Am I doing something totally wrong here?
>
> Gabor
>
> _______________________________________________
> Flask mailing list
> Flask at python.org
> https://mail.python.org/mailman/listinfo/flask
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/flask/attachments/20160422/3583432c/attachment.html>


More information about the Flask mailing list