-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Fix rescue_from subclasses default #651
Changes from 2 commits
196b0e1
14be585
ba410fa
acff701
462bd7c
bfb7b83
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,7 +39,7 @@ def call!(env) | |
end | ||
|
||
def find_handler(klass) | ||
handler = options[:rescue_handlers].find(-> { [] }) { |error, _| klass <= error }[1] | ||
handler = options[:rescue_handlers].find(-> { [] }) { |error, _| error.is_a?(Class) && klass <= error }[1] || options[:rescue_handlers][:all] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dblock Added the check for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems OK, however obscure if you don't have the context when reading this code. I think removing :all would have been clearer. Up to you if you want to try doing that or add a comment that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dblock Yeah, I was thinking of doing that. I don't know a method that can find only the first element matching and delete it for any There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe you can |
||
handler = options[:base_only_rescue_handlers][klass] || options[:base_only_rescue_handlers][:all] unless handler | ||
handler | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1343,6 +1343,18 @@ class ChildError < ParentError; end | |
expect { get '/uncaught_parent' }.to raise_error(StandardError) | ||
end | ||
|
||
it 'sets rescue_subclasses to true by default' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dblock I'm testing the behavior to see if it rescues subclasses, rather than testing if internally, it interprets There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is OK IMO. |
||
subject.rescue_from APIErrors::ParentError do |e| | ||
rack_response("rescued from #{e.class.name}", 500) | ||
end | ||
subject.get '/caught_child' do | ||
raise APIErrors::ChildError | ||
end | ||
|
||
get '/caught_child' | ||
expect(last_response.status).to eql 500 | ||
end | ||
|
||
it 'does not rescue child errors if rescue_subclasses is false' do | ||
subject.rescue_from APIErrors::ParentError, rescue_subclasses: false do |e| | ||
rack_response("rescued from #{e.class.name}", 500) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dblock The
!!
trick works when you wantnil, false
but since we wantnil, true
I ended up with a case statement. Do you know of a prettier more idiomatic way of doing this?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is totally fine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could just swap it around (
nil != false
):There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@adrianmacneil Yeah that would do the trick, thanks for the suggestion. In this case I'll stick with the
case
as it reads a bit clearer.