lotus/controller — pull request #44 by krzyzak (2014-10-02 13:21:27+00:00)
Title: Add custom exception handlers

With this code, you can handle your exceptions with custom logic (method or anything callable) – the only constraint is that it must return an array with status code and message.

This is still work in progress, just wanted to show my approach and current progress. I’ll update docs once everything else will be settled.

I’m still in doubt if if wouldn’t be more convinient to change one more function: `configuration.excepton_code`. At the moment it checks a hash for a key with exact match, which isn’t very flexible for me. I’d love to have something like this in my controller:

```ruby
handle_exception ApiReadTimeout => :api_read_timeout
handle_exception OtherException => :some_other_handler
handle_exception Exception => :generic_exception # this one catches everything else
```

To achieve this, I’d have to specify all possible exception classes, which of course is not an option.
Thus, I’d suggest doing something like this:

```ruby
def exception_code(exception)
@handled_exceptions.find{|ex| exception.is_a?(ex) }.to_a.last || DEFAULT_ERROR_CODE
end
```

What do you thing about such approach?