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

filter with raw exception instance #241

Merged
merged 6 commits into from
Jul 26, 2017
Merged

filter with raw exception instance #241

merged 6 commits into from
Jul 26, 2017

Conversation

kuboon
Copy link
Contributor

@kuboon kuboon commented Jul 21, 2017

README shows following example to ignore StandardError

Airbrake.add_filter do |notice|
  if notice[:errors].any? { |error| error[:type] == 'StandardError' }
    notice.ignore!
  end
end

But we rarely raise StandardError itself.
Instead, we raise the descendants of StandardError.

Current implementation converts the exception to a string value before filtering.
It looses class hierarchy.

This patch allows filter to use exception instance to do like this:

Airbrake.add_filter do |notice|
  notice.ignore! if notice.exception.kind_of? StandardError
end

Copy link
Contributor

@kyrylo kyrylo left a comment

Choose a reason for hiding this comment

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

This is a cool change 👍
I've been thinking about doing this, but there was no user demand, so I thought it wasn't needed.

It would be nice to add a test for the change, then I can merge the PR. Thanks!

README.md Outdated
if notice[:errors].any? { |error| error[:type] == 'StandardError' }
notice.ignore!
end
notice.ignore! if notice.exception.kind_of? StandardError
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use brackets around .kind_of?(StandardError). We prefer this style in this project.

@@ -62,7 +62,7 @@ class Notice
# @since v1.7.0
# @return [Hash{Symbol=>Object}] the hash with arbitrary objects to be used
# in filters
attr_reader :stash
attr_reader :stash, :exception
Copy link
Contributor

Choose a reason for hiding this comment

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

We should probably move this on a separate line because the comment above describes :stash.

@kuboon
Copy link
Contributor Author

kuboon commented Jul 24, 2017

Failed on Rubocop. How can I avoid it?

kyrylo added a commit that referenced this pull request Jul 25, 2017
We don't reuse them, so there's no need for them to be separate
methods. Besides, this will decrease class length, so we don't introduce
any Rubocop offences
(#241)
README.md Outdated
if notice[:errors].any? { |error| error[:type] == 'StandardError' }
notice.ignore!
end
notice.ignore! if notice.exception.kind_of?(StandardError)
Copy link
Contributor

Choose a reason for hiding this comment

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

Rubocop wants is_a? instead of kind_of?. They are aliases, so let's use is_a?.

@@ -514,6 +514,20 @@ def to_json(*)
@airbrake.notify_sync(ex)
expect(a_request(:post, endpoint)).to have_been_made.once
end

it "ignores descendant classes" do
class AirbrakeTestSubError < AirbrakeTestError; end
Copy link
Contributor

Choose a reason for hiding this comment

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

AirbrakeTestSubError can be a local variable:

descendant = Class.new(AirbrakeTestError)

It is preferred, so we don't pollute namespaces with constants.

@kyrylo
Copy link
Contributor

kyrylo commented Jul 25, 2017

To fix the Metrics/ClassLength, please rebase on master. I made some refactoring, which should help with this issue.

@@ -64,6 +64,10 @@ class Notice
# in filters
attr_reader :stash

##
# @return [Exception] raw exception
attr_reader :exception

Choose a reason for hiding this comment

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

what about putting this into @stash.exception?

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't see why not. @kuboon, that will probably the preferred solution. Let me know what you think.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

no problem. I did not understand what is stash for.

@kuboon
Copy link
Contributor Author

kuboon commented Jul 26, 2017

I can't see the detail of Rubocop fail.
image

@kyrylo
Copy link
Contributor

kyrylo commented Jul 26, 2017

You can run Rubocop locally.

Offenses:

lib/airbrake-ruby/notice.rb:77:16: C: Layout/SpaceInsideHashLiteralBraces: Space inside { missing. (https://github.com/bbatsov/ruby-style-guide#spaces-operators)
      @stash = {exception: exception}
               ^
lib/airbrake-ruby/notice.rb:77:37: C: Layout/SpaceInsideHashLiteralBraces: Space inside } missing. (https://github.com/bbatsov/ruby-style-guide#spaces-operators)
      @stash = {exception: exception}
                                    ^

@kyrylo
Copy link
Contributor

kyrylo commented Jul 26, 2017

Great work, thanks 👍

@kyrylo kyrylo merged commit 68599e8 into airbrake:master Jul 26, 2017
@kuboon kuboon deleted the patch-1 branch July 26, 2017 13:32
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.

3 participants