honeybadger-io / honeybadger-js

Universal JavaScript library for reporting errors to Honeybadger.io :zap:
https://www.honeybadger.io/for/javascript/
MIT License
109 stars 62 forks source link

Filtering breadcrumbs in a `beforeNotify` #1411

Open rabidpraxis opened 1 month ago

rabidpraxis commented 1 month ago

We had a customer reach out about filtering breadcrumbs, so I looked into recommending a beforeNotify snippet to remove unwanted breadcrumbs.

I could not get it to work, as the filtered breadcrumbs would not persist in the final payload. Now, I understand that accessing a field with double underscores (__breadcrumbs) is generally a no-no. Is there a way to do this that I am unaware of?

I pushed up a test in a branch with how I thought it would work:

https://github.com/honeybadger-io/honeybadger-js/compare/master...filter-breadcrumbs

subzero10 commented 3 weeks ago

Hey @rabidpraxis, the reason your approach didn't work is because beforeNotify callbacks act on the notice itself and breadcrumbs are only added to the request payload on a later stage. Your test in the branch didn't work because there is no n.__breadcrumbs variable; breadcrumbs are inside a client.__store variable.

The easiest workaround I can come up right now without introducing any changes would be to call Honeybadger.configure({ breadcrumbsEnabled: false }) to disable breadcrumb collection and then calling it again to enable it: Honeybadger.configure({ breadcrumbsEnabled: true }). Do you know if this would work for them?

In regards to your question about double underscores, it's one of the earlier ways to mark a field as private, but it's up to us whether we want to respect this (i.e. Javascript won't complain). In some occasions, it may be OK if we access these fields internally but we should not encourage their usage as part of the public SDK (i.e. we should not ask people to access it in a beforeNotify handler). I hope that makes sense.