localshred/protobuf — pull request #36 by localshred (2012-11-08 22:12:35+00:00)
Title: Add rescue_from support to filters

Rescue filters can be addressed by registering filters with `rescue_from`. Rescue from differs from a simple around filter in that it will run around the entire service method, including before and after filters. Around filters run between the before and after filters and hence won't truly catch everything that the service may raise.

Rescue from will not, however, be run in the context of the server process or any (de)serialization routines. Keep that in mind. As is the case with filters, rescue filters will be run in the context of the service instance, so you have access to `respond_with`, `rpc_failed`, `request`, `response`, as well as any instance-level state.

Define the rescue filter by providing the exception class (or classes) you wish to handle, and then an instance method name via the `:with` param.

```ruby
class Foo::UserService
rescue_from ActiveRecord::RecordNotFound, :with => :record_not_found

def show
# ... some code that throws the above exception but does not explicitly handle it
end

private

def record_not_found(ex)
log_error { ex.message }
log_debug { ex.backtrace.join("\n") }
end
end
```

Alternatively you can provide a callable object to `:with` or simply pass a block. In this case you should accept two parameters to your block, the service object and the exception.

```ruby
class Foo::UserService
rescue_from ActiveRecord::RecordNotFound do |service, ex|
log_error { ex.message }
log_debug { ex.backtrace.join("\n") }
end

# ... snip
end
```

-------

Thoughts?