Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle ordering of flask error handlers

How does one ensure that the flask error handlers get the most specific exception?

From some simple tests and looking at the source code, it looks like the flask error handling code just takes the first register error handler for a given exception type instead of the most specific type possible.

I guess the answer is to put the error handler for Exception at the very end?

like image 806
oxer Avatar asked Sep 02 '25 15:09

oxer


1 Answers

Error handlers follow the exception class MRO, or method resolution order, and a handler is looked up in that order; the specific exception type first, then it's direct parent class, etc, all the way down to BaseException and object.

There is no need to order anything; if you registered a handler for Exception, then it'd be used for any exception for which no more specific handler was found.

like image 189
Martijn Pieters Avatar answered Sep 04 '25 06:09

Martijn Pieters