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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Honeybadger.context returns Honeybadger::Agent when passing a block #545

Closed
d4rky-pl opened this issue May 10, 2024 · 0 comments 路 Fixed by #546
Closed

Honeybadger.context returns Honeybadger::Agent when passing a block #545

d4rky-pl opened this issue May 10, 2024 · 0 comments 路 Fixed by #546

Comments

@d4rky-pl
Copy link
Contributor

d4rky-pl commented May 10, 2024

This is something that I missed when implementing #541 (sorry! 馃檹) that may be annoying to people wanting to use the local contexts: If we pass a block to Honeybadger.context I would expect the return value to be the result of the block, rather than the Honeybadger::Agent instance.

Imagine a case we have a critical payments-related process in our application and we want to make sure that all errors triggered in this process are tagged. Pseudocode:

class SuperImportantPaymentProcessing
  def call
    # bunch of things happening
    { success: true, charge: }
  end
end

With the current implementation we need to either create additional variable before the block to make sure the return value from #call remains the same:

class SuperImportantPaymentProcessing
  def call
    result = nil
    Honeybadger.context({ tags: ['critical', 'payments'] }) do
      # bunch of things happening
      result = { success: true, charge: }
    end
    result
  end
end

or we have to use return explicitly:

class SuperImportantPaymentProcessing
  def call
    Honeybadger.context({ tags: ['critical', 'payments'] }) do
      # bunch of things happening
      return { success: true, charge: }
    end
  end
end

While it still works, it feels unexpected. I'm not sure if there's any actual API level expectation that Honeybadger.context returns Agent instance (it's undocumented which is why I missed it) but I think it could make sense to modify the behaviour. I just noticed that the reason it returns Agent instance is to allow chaining, e.g. Honeybadger.context.clear!. I think it makes sense without the block but I would still modify the behaviour for the block version.

Having the behaviour modified would also pair with rescue blocks well, e.g.

class SuperImportantPaymentProcessing
  def call
    Honeybadger.context({ tags: ['critical', 'payments'] }) do
      # bunch of things happening
      { success: true, charge: }
    rescue PaymentError => e
      Honeybadger.notify(e) # this still gets tagged!
      { success: false }
    end
  end
end

Let me know what you think and I can prep the PR with a fix.

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 a pull request may close this issue.

1 participant