Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Catching a 500 server error in Flask

Tags:

python

flask

I love Flask's error catching. It's beautifully simple:

@app.errorhandler(404) def pageNotFound(error):     return "page not found" 

works like charm. But it doesn't work for the 500 error code. I want to catch Python errors when something goes wrong an exception is raised in the code. Is that possible?

I should note that if I explicitly call return abort(500) in a view then the 500 errorhandler does work. So this is explicitly for when the Python code fails.

Is this possible?

like image 450
J-bob Avatar asked Feb 21 '13 01:02

J-bob


People also ask

How do I fix a 500 Internal server error?

Clear your browser cache and cookies Check these articles on deleting the cache on an Android phone or iPhone, if you use a mobile device. Alternatively, you can test opening the page from another browser. For instance, if you use Chrome, try Firefox or vice versa.

How do you catch a flask error?

When an error occurs in Flask, an appropriate HTTP status code will be returned. 400-499 indicate errors with the client's request data, or about the data requested. 500-599 indicate errors with the server or application itself. You might want to show custom error pages to the user when an error occurs.

How does Python handle 500 internal server error?

Make sure debug mode is off, then try again. Here is a comment directly from the code itself: Default exception handling that kicks in when an exception occurs that is not caught. In debug mode the exception will be re-raised immediately, otherwise it is logged and the handler for a 500 internal server error is used.

How do I fix an Internal server error in python flask?

Step 1 — Using The Flask Debugger. In this step, you'll create an application that has a few errors and run it without debug mode to see how the application responds. Then you'll run it with debug mode on and use the debugger to troubleshoot application errors.


1 Answers

What you have described is, by default, how Flask works. My assumption is that you are running in debug mode, and therefore exceptions are being shown to you in the debug screen. Make sure debug mode is off, then try again. Here is a comment directly from the code itself:

Default exception handling that kicks in when an exception occurs that is not caught. In debug mode the exception will be re-raised immediately, otherwise it is logged and the handler for a 500 internal server error is used. If no such handler exists, a default 500 internal server error message is displayed.

like image 92
Mark Hildreth Avatar answered Sep 18 '22 19:09

Mark Hildreth