Skip to content

Commit

Permalink
Merged in wbinnssmith/analytics-unhandled-rejection (pull request #68)
Browse files Browse the repository at this point in the history
Prevent analytics-node from creating unhandled promise rejections and crashing Parcel

Approved-by: Marcin Szczepanski
  • Loading branch information
Will Binns-Smith committed Mar 23, 2022
2 parents 983eb5a + 76713d7 commit aa6dcd3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
Expand Up @@ -15,6 +15,7 @@
"@atlassiansox/analytics-node-client": "^2.2.2",
"@parcel/hash": "2.0.20",
"@parcel/plugin": "2.0.20",
"analytics-node": "^5.2.0",
"execa": "^5.1.1",
"uuid": "^3.3.3"
}
Expand Down
20 changes: 17 additions & 3 deletions packages/reporters/atlassian-reporter-analytics/src/analytics.js
Expand Up @@ -2,12 +2,26 @@

// flowlint-next-line untyped-import:off
import {analyticsClient, userTypes} from '@atlassiansox/analytics-node-client';
// flowlint-next-line untyped-import:off
import SegmentAnalytics from 'analytics-node';
import os from 'os';
import {hashString} from '@parcel/hash';
import getMachineModel from './getMachineModel';

const HASHED_EMAIL = hashString(`${os.userInfo().username}@${os.hostname()}`);

// Monkeypatch SegmentAnalytics not to create unhandled promise rejections.
// TODO: Remove when https://github.com/segmentio/analytics-node/issues/326
// is resolved
const originalSegmentAnalyticsFlush = SegmentAnalytics.prototype.flush;
SegmentAnalytics.prototype.flush = function flush(callback, ...rest) {
return originalSegmentAnalyticsFlush
.call(this, callback, ...rest)
.catch(err => {
callback(null, err);
});
};

let client;
if (
process.env.PARCEL_BUILD_ENV === 'production' &&
Expand Down Expand Up @@ -74,13 +88,13 @@ const analytics = {

if (client != null) {
try {
return await client.sendTrackEvent(trackEvent);
await client.sendTrackEvent(trackEvent);
} catch (err) {
if (process.env.ANALYTICS_DEBUG != null) {
// Don't let a failure to report analytics crash Parcel
if (process.env.PARCEL_ANALYTICS_DEBUG != null) {
// eslint-disable-next-line no-console
console.error('Failed to send analytics', err);
}
// Don't let a failure to report analytics crash Parcel
}
}
},
Expand Down

0 comments on commit aa6dcd3

Please sign in to comment.