keep request-specific error handling behavior (e.g. flask.abort or otherwise responding to a request) as close the request handler as possible and decoupled from any modules that may be called outside of a request contexty
e.g. I put some flask.abort(404) calls in a data_manager file called both during request + during admin tasks like rebuilding the app’s cache storage bucket (where flask is not relevant and a request is not in progress)
Create a custom error class:
Register an error handler:
How do I get this to actually catch the exceptions and call the handler before they become 500s?
Don’t wrap handler code with try/except
If you do, the error won’t bubble up to the handler
There’s no need for a blanket try/except Exception as flask will already catch those for you as 500 responses
So, if your error handler isn’t being called and you’re still getting 500, remove any try/except wrapping your handler body
Or catch the custom exception and call the error handler explicitly?
Which is better? Is one better in all cases? Catching seems to be working better…