Skip to content

Commit

Permalink
EPMRPP-85867 || Reporting is down on error with GA4 (#170)
Browse files Browse the repository at this point in the history
- Add try/cath block
- Add unit tests
- Add new es-lint rule to force handle promises results
  • Loading branch information
Bam6ycha authored Sep 29, 2023
1 parent bd6a15a commit 92bc1d1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 19 deletions.
6 changes: 3 additions & 3 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
"sourceType": "module",
"project": "./tsconfig.eslint.json"
},
"globals": {
"expectAsync": true
Expand All @@ -19,8 +20,6 @@
"jasmine": true
},
"rules": {
"indent": ["error", 2],
"max-len": ["error", 120],
"valid-jsdoc": ["error", { "requireReturn": false }],
"consistent-return": 0,
"@typescript-eslint/no-plusplus": 0,
Expand All @@ -35,6 +34,7 @@
"@typescript-eslint/dot-notation": 0,
"@typescript-eslint/ban-ts-comment": 0,
"@typescript-eslint/no-var-requires": 0,
"@typescript-eslint/no-floating-promises": 2,
"max-classes-per-file": 0
}
}
6 changes: 3 additions & 3 deletions lib/report-portal-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,11 @@ class RPClient {
return RestClient.request('GET', url, {}, { headers: this.headers });
}

triggerStatisticsEvent() {
async triggerStatisticsEvent() {
if (process.env.REPORTPORTAL_CLIENT_JS_NO_ANALYTICS) {
return;
}
this.statistics.trackEvent();
await this.statistics.trackEvent();
}

/**
Expand Down Expand Up @@ -206,7 +206,7 @@ class RPClient {
);
});
}
this.triggerStatisticsEvent();
this.triggerStatisticsEvent().catch(console.error);
return {
tempId,
promise: this.map[tempId].promiseStart,
Expand Down
12 changes: 12 additions & 0 deletions spec/statistics.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,17 @@ describe('Statistics', () => {

expect(axios.post).toHaveBeenCalledOnceWith(url, baseRequestValidation);
});

it('Should properly handle errors if any', async () => {
const statistics = new Statistics(eventName, agentParams);
const errorMessage = 'Error message';

spyOn(axios, 'post').and.throwError(errorMessage);
spyOn(console, 'error');

await statistics.trackEvent();

expect(console.error).toHaveBeenCalledWith(errorMessage);
});
});
});
30 changes: 17 additions & 13 deletions statistics/statistics.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,24 @@ class Statistics {
}

async trackEvent() {
const requestBody = {
client_id: await getClientId(),
events: [
{
name: this.eventName,
params: this.eventParams,
},
],
};
try {
const requestBody = {
client_id: await getClientId(),
events: [
{
name: this.eventName,
params: this.eventParams,
},
],
};

await axios.post(
`https://www.google-analytics.com/mp/collect?measurement_id=${MEASUREMENT_ID}&api_secret=${API_KEY}`,
requestBody,
);
await axios.post(
`https://www.google-analytics.com/mp/collect?measurement_id=${MEASUREMENT_ID}&api_secret=${API_KEY}`,
requestBody,
);
} catch (error) {
console.error(error.message);

Check warning on line 47 in statistics/statistics.js

View workflow job for this annotation

GitHub Actions / test (10)

Unexpected console statement

Check warning on line 47 in statistics/statistics.js

View workflow job for this annotation

GitHub Actions / test (12)

Unexpected console statement

Check warning on line 47 in statistics/statistics.js

View workflow job for this annotation

GitHub Actions / test (14)

Unexpected console statement

Check warning on line 47 in statistics/statistics.js

View workflow job for this annotation

GitHub Actions / test (16)

Unexpected console statement

Check warning on line 47 in statistics/statistics.js

View workflow job for this annotation

GitHub Actions / test (18)

Unexpected console statement

Check warning on line 47 in statistics/statistics.js

View workflow job for this annotation

GitHub Actions / test (20)

Unexpected console statement
}
}
}

Expand Down

0 comments on commit 92bc1d1

Please sign in to comment.