Skip to content

feat: add a notice class and before_notify hook #203

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

Merged
merged 8 commits into from
May 8, 2025

Conversation

roelbondoc
Copy link
Contributor

@roelbondoc roelbondoc commented May 5, 2025

The main goal of this PR was to add a before_notify hook as part of the configuration. This allows the library to be configured to intercept, inspect, and alter a error notice before it is sent off to Honeybadger.

To make this easier, a new Notice class has been added so that there is a single object that gets passed into the hook. The payload is generate upon instantiation and can be later inspected or altered in the hook.

This preemptive generation isn't ideal because it's a little cumbersome to work with the notice payload. However, the plugins implement the generate_payload method which takes in the formatted notice payload. After this, I'd like to take a stab at making it easier to manipulate a notice without having to dig into the final payload.

The payload is a cached property allowing the before_notify hook to alter the payload before sending.

The before_notify hook must return the notice in order for the notice to be sent. A None value will halt the sending.

Closes: #187 and #185

@roelbondoc roelbondoc force-pushed the add-before-notify-hook branch 3 times, most recently from 74ff496 to 07d1cc2 Compare May 6, 2025 15:53
@roelbondoc roelbondoc changed the title Add a notice class and before_notify hook feat: add a notice class and before_notify hook May 6, 2025
@roelbondoc roelbondoc force-pushed the add-before-notify-hook branch from 07d1cc2 to f1a0295 Compare May 6, 2025 18:02
Base automatically changed from context-tags to master May 6, 2025 18:30
@roelbondoc roelbondoc force-pushed the add-before-notify-hook branch from f1a0295 to 289cbf8 Compare May 6, 2025 18:40
The main goal of this PR was to add a `before_notify` hook as part of the configuration. This allows the library to be configured to intercept, inspect, and alter a error notice before it is sent off to Honeybadger.

To make this easier, a new `Notice` class has been added so that there is a single object that gets passed into the hook. The payload is generate upon instantiation and can be later inspected or altered in the hook.

This preemptive generation isn't ideal because it's a little cumbersome to work with the notice payload. However, the plugins implement the `generate_payload` method which takes in the formatted notice payload. After this, I'd like to take a stab at making it easier to manipulate a notice without having to dig into the final payload.
@roelbondoc roelbondoc force-pushed the add-before-notify-hook branch from 289cbf8 to e6b47ee Compare May 6, 2025 20:04
@roelbondoc roelbondoc marked this pull request as ready for review May 6, 2025 20:18
@roelbondoc roelbondoc requested a review from a team May 6, 2025 20:18
@rabidpraxis
Copy link
Contributor

rabidpraxis commented May 6, 2025

I know we are looking for parity with the Ruby client, but what if we switch from side‐effecting halt() to a return‐value API where:

  • returning None drops the notice
  • returning a (mutated or new) notice continues

I think it would also simplify things to remove the filtering logic out of the Notice class and into notify() flow, along with the skipping logic of before_notify.

Thoughts?

@roelbondoc
Copy link
Contributor Author

I know we are looking for parity with the Ruby client, but what if we switch from side‐effecting halt() to a return‐value API where:

  • returning None drops the notice
  • returning a (mutated or new) notice continues

I think it would also simplify things to remove the filtering logic out of the Notice class and into notify() flow, along with the skipping logic of before_notify.

Thoughts?

I think that makes a lot of sense! Let me refactor and add some tests.

Copy link
Member

@stympy stympy left a comment

Choose a reason for hiding this comment

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

Looking good! Perhaps we should open another issue for changing the logging to structured logging. :)

@roelbondoc roelbondoc linked an issue May 8, 2025 that may be closed by this pull request
@roelbondoc roelbondoc requested a review from rabidpraxis May 8, 2025 14:39
Copy link
Contributor

@rabidpraxis rabidpraxis left a comment

Choose a reason for hiding this comment

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

This looks good!

I have one recommendation to move the tags tests from test_core to test_notice.


logging.getLogger("honeybadger").addHandler(logging.NullHandler())
logger = logging.getLogger("honeybadger")
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe removing the null handler will attach the logger to the root.

https://github.com/honeybadger-io/honeybadger-python?tab=readme-ov-file#logging

@roelbondoc roelbondoc merged commit b39ce59 into master May 8, 2025
35 checks passed
@roelbondoc roelbondoc deleted the add-before-notify-hook branch May 8, 2025 19:43
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.

honeybadger.notify: error_message is ignored when an exception is provided feat: add support for before_notify handlers
3 participants