Feature: Use self-replying exceptions in ActiveSupport::Rescuable.rescue_from

51 views
Skip to first unread message

Arnaud Rouyer

unread,
May 15, 2015, 11:44:18 AM5/15/15
to rubyonra...@googlegroups.com
I'm using a library to handle error responses across five different API apps. To stop all processing, this library raises an error that is later rescued with `rescue_from`.

Since DRY is important to me, I also want to avoid repeating the `rescue_from` call and its handler block in all five applications. Furthermore, if I someday decide to change the handler, I would need to update all five applications.

Solution I've found is to extend my error class with a `to_proc` method that generates a proc handling the response in the controller (in this case: `render json: exception.reply`). I've patched rails to this end: `rescue_from` can now be called without a block handler, in which case the block handler will be the proc of the exception.

Full diff patch with tests is here: https://gist.github.com/joshleaves/10de348b0e8b28863213

I'm waiting for feedback before submitting the PR. Thanks =)

Matt Jones

unread,
May 15, 2015, 7:35:04 PM5/15/15
to rubyonrails-core
IMO, the more idiomatic way to handle this would be to have a concern that sets up these rescues defined in your library, then include it into controllers in the clients as needed. Something like this:


--Matt Jones
Reply all
Reply to author
Forward
0 new messages