Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to pass method name to :with option of rescue_from #1257

Closed
wants to merge 2 commits into from

Conversation

namusyaka
Copy link
Contributor

Closes #790

I think the feature is useful for writing error handling without block.
Thoughts?

end

rescue_from :all, with: :server_error!
rescue_from ArgumentError, with: Rack::Response.new('rescued with a method', 400)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think with: :method is very useful, however a rescue with a Rack::Response is probably dangerous, because the instance of the Rack::Response is constructed at load time. This tends to cause all kinds of wonderful behavior like memory leaks, people are reckless with them. I think the above should not be allowed, you should do a -> () { Rack...} when you want to return something like this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, you're right. But the dangerous behaviour is expected.
Should I remove that and introduce lambda approach?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that yes. You should either use a symbol or a proc here.

@namusyaka namusyaka force-pushed the implement-with-option branch from 30fb7c7 to 114cc10 Compare January 27, 2016 04:10
@namusyaka
Copy link
Contributor Author

Force pushed.
@dblock Can you take a look at the changes?

@@ -10,6 +10,8 @@
* [#1243](https://github.com/ruby-grape/grape/pull/1243): Add `header` support for middleware - [@namusyaka](https://github.com/namusyaka).
* [#1252](https://github.com/ruby-grape/grape/pull/1252): Allow default to be a subset or equal to allowed values without raising IncompatibleOptionValues - [@jeradphelps](https://github.com/jeradphelps).
* [#1255](https://github.com/ruby-grape/grape/pull/1255): Allow param type definition in route_param - [@namusyaka](https://github.com/namusyaka)
* [#1257](https://github.com/ruby-grape/grape/pull/1257): Allow to pass method name to :with option of rescue_from - [@namusyaka](https://github.com/namusyaka)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We didn't have with: before, right? So both of these are the same change, which is "Added rescue_from with: ..." that can take a method name or a proc.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nevermind, we did, it's early in the AM ;)

@dblock
Copy link
Member

dblock commented Jan 27, 2016

One last thing we need a test (and possibly a fix for) is when you mix a block and with:. I suppose it should raise an exception as well.

rescue_from ArgumentError, with: error_handler do | ... |
  # is this called?
end

@namusyaka namusyaka force-pushed the implement-with-option branch from 114cc10 to 2575bc1 Compare January 27, 2016 13:46
@namusyaka
Copy link
Contributor Author

Done. Thank you for your review!
I think both :with option and block should not be passed, too.

@namusyaka
Copy link
Contributor Author

@dblock Let me know if you have any problems.

@dblock
Copy link
Member

dblock commented Jan 29, 2016

merged via 110c4f1. I just edited the CHANGELOG a bit. Nice work @namusyaka, thanks!

@dblock dblock closed this Jan 29, 2016
@namusyaka namusyaka deleted the implement-with-option branch January 30, 2016 17:47
@namusyaka
Copy link
Contributor Author

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants