diff --git a/.eslintrc b/.eslintrc index d71ab5a..3b8c3c6 100644 --- a/.eslintrc +++ b/.eslintrc @@ -8,7 +8,8 @@ "parser": "@typescript-eslint/parser", "parserOptions": { "ecmaVersion": 2018, - "sourceType": "module" + "sourceType": "module", + "project": "./tsconfig.eslint.json" }, "globals": { "expectAsync": true @@ -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, @@ -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 } } diff --git a/lib/report-portal-client.js b/lib/report-portal-client.js index e95eb46..b76de1d 100644 --- a/lib/report-portal-client.js +++ b/lib/report-portal-client.js @@ -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(); } /** @@ -206,7 +206,7 @@ class RPClient { ); }); } - this.triggerStatisticsEvent(); + this.triggerStatisticsEvent().catch(console.error); return { tempId, promise: this.map[tempId].promiseStart, diff --git a/spec/statistics.spec.js b/spec/statistics.spec.js index 3979316..277eef1 100644 --- a/spec/statistics.spec.js +++ b/spec/statistics.spec.js @@ -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); + }); }); }); diff --git a/statistics/statistics.js b/statistics/statistics.js index 076d7e7..56042ee 100644 --- a/statistics/statistics.js +++ b/statistics/statistics.js @@ -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); + } } }