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

Updates analytics error handling to support checking delivery failures on Context #505

Merged
merged 8 commits into from
Jul 8, 2022

Conversation

chrisradek
Copy link
Contributor

This PR attempts to standardize how plugin errors impact analytics API calls and provides a straightforward way to check if event delivery failed.

Background

Errors in enrichment, destination, and after plugins don't cause analytics calls to throw. before plugins do, but what they throw depends on if retries are enabled or not. If there are no retries, their errors bubble up to the analytics call. If there are retries, the event's Context itself gets thrown.
To further complicate things, source middleware (implemented as a before plugin) does not bubble errors if next is never called, but does otherwise.

Only before plugins and source middleware errors can cause event delivery to fail entirely, but the inconsistent behavior makes it difficult to write code that can react to these failures.

Changes

The following has been changed:

  • all plugins now act consistently in that errors are no longer bubbled up to the analytics calling code (e.g. analytics.track())
  • before plugins now support cancelation: this is a bug fix, existing test missed that this was broken
  • Context.failedDelivery() is added, so now the following works:
    const context = await analytics.track('event');
    if (context.failedDelivery()) {
      // log failure, retry, etc
    }

This also has the benefit that callbacks can now be invoked even on failures, so you can do:

analytics.track('event', (context) => {
  if (context.failedDelivery()) {
    // log failure, retry, etc
  }
})

Previously callbacks weren't being invoked if the analytics call threw.

Chore

Also updated the vscode launch.json so the actions work in packages/browser.

} from './test-plugins'

describe('Analytics', () => {
describe('plugin error behavior', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

amazing 🙌

@chrisradek
Copy link
Contributor Author

Just realized I missed one scenario - before plugin fails the 1st time then succeeds on retry. Will need to update the context in this case so that context.getFailedDelivery() does not return anything.

@changeset-bot
Copy link

changeset-bot bot commented Jun 30, 2022

🦋 Changeset detected

Latest commit: 62e445e

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@segment/analytics-next Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@chrisradek chrisradek merged commit b9c6356 into master Jul 8, 2022
@chrisradek chrisradek deleted the fix-plugin-before branch July 8, 2022 20:09
@github-actions github-actions bot mentioned this pull request Jul 8, 2022
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.

2 participants