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

Add support for custom exception attributes #113

Merged
merged 1 commit into from
Sep 2, 2016

Conversation

kyrylo
Copy link
Contributor

@kyrylo kyrylo commented Aug 26, 2016

Fixes #112 (Adding to the 'params' area of airbrake.io when
Airbrake.notify not explicitly called)


# The library expects you to define this method. You must return a Hash,
# containing the keys you want to modify.
def to_airbrake

Choose a reason for hiding this comment

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

Why not to use some standard Ruby method like to_hash and/or to_json?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

When somebody defines a method with this name it's already clear why. It also must correspond to our API scheme, so if this was to_hash, I would expect it to return this instead: { http_code: 404 }. But we need the params key, for example.

Copy link

@vmihailenco vmihailenco Aug 26, 2016

Choose a reason for hiding this comment

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

I am not convinced. You use to_hash or something like that to serialize arrays, hashes and classes. You can just set notice.params[exception] = exc and let serializer do its job.

But we need the params key, for example.

Do you have any other examples?

Copy link
Contributor Author

@kyrylo kyrylo Aug 26, 2016

Choose a reason for hiding this comment

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

I understand what you want to achieve, but:

  1. A Hash must be returned, so we could merge it with the Notice object, so it could be filtered & truncated, if needed
  2. We could achieve what you suggest only if we allowed populating the "params" bit of the payload (ignoring context, session and the rest), but how useful would that be? I think we could afford it for the sake of simpler implementation, but we still can't overcome (1)
  3. How can we detect if we should append the exception to the Notice object? Currently we check if to_airbrake is defined. With your proposed API this won't be possible and we would need to do notice.params[exception] = exc for every exception, which doesn't look good. By default, to_json on exception object seems to be calling to_s, so if the user doesn't define it, then we get a string like:
    "params":{"exception":"\"MyException\""} instead of
    "params":{"exception":{"foo":"bar"}}. Because of Rails' ActiveSupport your idea would work, but for vanilla Ruby it doesn't work like that, unfortunately.

P.S. This is how the exception would look like

class MyException < StandardError do
  def initialize(params)
    @params = params
  end
end

ex = MyException.new(foo: 'bar')

# Vanilla Ruby
ex.to_json #=> "\"MyException\""

# Rails
ex.to_json #=> "{\"foo\":\"bar\"}"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am going to merge this today unless you object.

Fixes #112 (Adding to the 'params' area of airbrake.io when
Airbrake.notify not explicitly called)
@kyrylo kyrylo force-pushed the custom-exceptions-support branch from cb87ba9 to 968e54e Compare August 26, 2016 13:39
@hale
Copy link

hale commented Aug 31, 2016

This is really great @kyrylo, thanks. Integrating with the branch at the moment - will let you know when I've had a chance to test end to end.

@hale
Copy link

hale commented Aug 31, 2016

Looking good - data defined by to_airbrake is passed into Airbrake from the custom exception as expected. Appreciate it!

@kyrylo
Copy link
Contributor Author

kyrylo commented Sep 1, 2016

Awesome, I think this change will go in, but as for the next release, I can't promise it today or tomorrow (although it's possible). So, I'd say sometime next week.

@kyrylo kyrylo merged commit b2f9f3b into master Sep 2, 2016
@kyrylo kyrylo deleted the custom-exceptions-support branch September 2, 2016 15:20
kyrylo added a commit that referenced this pull request Sep 6, 2016
The library supports custom exception attributes. This is useful if you work
with custom exceptions, which define non-standard attributes and you can't
attach any additional data with help of the [`add_filter`](#airbrakeadd_filter)
API due to the fact that the data isn't available at confuration time yet.
Copy link

Choose a reason for hiding this comment

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

spelling: confuration -> configuration

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you, fixed!

kyrylo added a commit that referenced this pull request Sep 28, 2016
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.

4 participants