[Rails-core] Feature: Use self-replying exceptions in ActiveSupport::Rescuable.rescue_from

2015-05-15 Thread Arnaud Rouyer
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 =)

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-core+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-core@googlegroups.com.
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.


Re: [Rails-core] Feature: Use self-replying exceptions in ActiveSupport::Rescuable.rescue_from

2015-05-15 Thread Matt Jones
On Fri, May 15, 2015 at 6:15 AM, Arnaud Rouyer 
wrote:

> 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:

https://gist.github.com/al2o3cr/3e1c492af329add23f30

--Matt Jones

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to rubyonrails-core+unsubscr...@googlegroups.com.
To post to this group, send email to rubyonrails-core@googlegroups.com.
Visit this group at http://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.