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

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 =)

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