Skip to content
This repository has been archived by the owner on Feb 13, 2024. It is now read-only.

Add an errorHandler property to constructor options #342

Merged
merged 2 commits into from Jun 22, 2022
Merged

Add an errorHandler property to constructor options #342

merged 2 commits into from Jun 22, 2022

Conversation

ghost
Copy link

@ghost ghost commented Jun 17, 2022

✔️ All existing tests pass
✔️ New functionality is unit-tested

Addresses #320 #326

What this adds

This PR adds an optional errorHandler property to the class constructor's options.
If unspecified, the behaviour of the library does not change.
If specified, when an axios request fails, errorHandler(axiosError) will be called instead of re-throwing the axios error.

Example usage:

const Analytics = require('analytics-node');

const client = new Analytics('write key', {
  errorHandler: (err) => {
    console.error('analytics-node flush failed.')
    console.error(err)
  }
});

client.track({
  event: 'event name',
  userId: 'user id'
});
// ☝️ if this fails when flushed no exception will be thrown and instead
// the axios error will be logged to the console.

Why

Since no way to handle the re-thrown axios error was exposed previously, this meant that if the axios request failed for any reason, the application using analytics-node would crash. A workaround was to use node's process unhandledRejection or unhandledException handlers, but this is very un-ergonomic for library users.

Work remaining

This functionality needs to be documented: https://segment.com/docs/connections/sources/catalog/libraries/server/node/

Tam CARRE and others added 2 commits June 17, 2022 12:22
…n return a value using this error handler in case of axios exception instead of throwing. unit test included.
@pooyaj pooyaj self-requested a review June 22, 2022 17:40
Copy link
Contributor

@pooyaj pooyaj left a comment

Choose a reason for hiding this comment

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

Looks great 🙌

@nd4p90x
Copy link
Collaborator

nd4p90x commented Jun 23, 2022

PR For Node Documentation Update:
segmentio/segment-docs#3111

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants