Shopify/batman — pull request #538 by kristianpd (2012-08-23 01:22:38+00:00)
Title: Improved error handling through catchError

This pull request implements a first run at improved error handling, similar to `rescue_from` in rails.

# Batman.RestStorage changes

The following `Error` classes have been added to `Batman.StorageAdapter` and in turn the `Batman.RestStorage` adapter has also been extended to improve error reporting for common HTTP status code errors. The following errors now have their own respective error classes:

* 404 - `NotFoundError`
* 406 - `NotAcceptableError`
* 422 - `UnprocessableEntityError`
* 500 - `InternalStorageError`
* 501 - `NotImplementedError`

# Batman.Controller changes
From a controller point of view, we now have the ability to register for specific types of errors and provide callbacks to be executed if such an error occurs. The call to the storage adapter must be wrapped in a `errorHandler` callback that will ensure the error callbacks are fired.

In the following example:

```coffee
class FooController extends Batman.Controller
logErrors: (err) ->
console.log('All StorageErrors: ', err)

log404and500errors: (err) ->
console.log('Only 500 errors: ', err)

logToServer: (err) ->
logger.log(err)

@catchError Batman.StorageAdapter.StorageError, with: @::logErrors
@catchError Batman.StorageAdapter.NotFoundError, Batman.StorageAdapter.InternalStorageError, with: [@::log404and500errors, @::logToServer]

index: ->
Model.load @errorHandler (results) ->
console.log('Successful and no errors', results)
```
* All `StorageErrors` will fire `logErrors`
* Any 404 or 500 status codes will fire both `log404and500errors` and `logToServer`

# Notes

* Subclassing controllers will merge all handlers for the subclassed controller and all ancestors
* Any subclass of an `Error` registered with `@catchError` will pass the condition

@hornairs this isn't 100% yet but your thoughts so far?