Here is another thing that falls into the category of Things I’ve Learned, fresh from Batman 0.13.0:
- Add
Controller::handleError(),Controller::errorHandler(), andController.catchErrorfor regstering error handlers and wrapping callbacks to automatically handle errors coming back from async operations.
This nifty addition from kristianpd and the fine folks at Shopify makes it so you can have rescue_from -like functionality in your Batman controllers.
Let’s say you are concerned about 404s, which is a pretty reasonable status code to handle under all circumstances. You would start by registering the error handling function and your intent to handle it:
1 2 3 4 | |
Now we diverge from rescue_from a little bit…you need to wrap the callback which throws the error in order to handle it. So, replace your lookup:
1 2 3 4 5 | |
With one that describes your intent to handle errors thrown in that block:
1 2 3 4 5 | |
You can find the basic status code errors in the StorageAdapter but that’s just an example of using this for storage related errors. You are free to make all sorts of custom errors and handle them just as you would these ones.