@google-cloud/nodejs-common 1.5.4-beta → 1.5.6-beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@google-cloud/nodejs-common",
3
- "version": "1.5.4-beta",
3
+ "version": "1.5.6-beta",
4
4
  "description": "A NodeJs common library for solutions based on Cloud Functions",
5
5
  "author": "Google Inc.",
6
6
  "license": "Apache-2.0",
@@ -25,10 +25,14 @@ const {
25
25
  getLogger,
26
26
  SendSingleBatch,
27
27
  BatchResult,
28
+ wait,
28
29
  } = require('../components/utils.js');
29
30
  /** Base URL for Google Analytics service. */
30
31
  const BASE_URL = 'https://www.google-analytics.com';
31
32
 
33
+ /** @const{number} Times to retry when server responds an error. */
34
+ const RETRY_TIMES = 2;
35
+
32
36
  /**
33
37
  * Configuration for Measurement Protocol GA4.
34
38
  * @see https://developers.google.com/analytics/devguides/collection/protocol/ga4/reference#payload_post_body
@@ -109,40 +113,50 @@ class MeasurementProtocolGA4 {
109
113
  .map((param) => `${param}=${params[param]}`)
110
114
  .join('&');
111
115
  },
116
+ validateStatus: () => true,
112
117
  body: JSON.stringify(hit),
113
- headers: {'User-Agent': 'Tentacles/MeasurementProtocol-GA4'}
118
+ headers: { 'User-Agent': 'Tentacles/MeasurementProtocol-GA4' },
114
119
  };
115
- const response = await request(requestOptions);
116
120
  /** @type {BatchResult} */ const batchResult = {
117
121
  numberOfLines: lines.length,
118
122
  };
119
- if (response.status < 200 || response.status >= 300) {
120
- const errorMessages = [
121
- `Measurement Protocol GA4 [${batchId}] didn't succeed.`,
122
- `Get response code: ${response.status}`,
123
- `response: ${response.data}`,
124
- ];
125
- this.logger.error(errorMessages.join('\n'));
126
- batchResult.errors = errorMessages;
127
- batchResult.result = false;
128
- return batchResult;
129
- }
123
+ let retriedTimes = 0;
124
+ let response;
125
+ while (retriedTimes <= RETRY_TIMES) {
126
+ response = await request(requestOptions);
127
+ if (response.status < 500) break; // Only retry when server errors.
128
+ this.logger.warn('Got an 5XX error', response);
129
+ retriedTimes++;
130
+ await wait(retriedTimes * 1000);
131
+ };
130
132
  this.logger.debug('Configuration:', config);
131
133
  this.logger.debug('Input Data: ', lines);
132
134
  this.logger.debug(`Batch[${batchId}] status: ${response.status}`);
133
135
  this.logger.debug(response.data);
134
136
  // There is not enough information from the non-debug mode.
135
137
  if (!this.debugMode) {
136
- batchResult.result = true;
138
+ if (response.status >= 200 && response.status < 300) {
139
+ batchResult.result = true;
140
+ } else {
141
+ batchResult.result = false;
142
+ const errorMessage =
143
+ `MP GA4 [${batchId}] http status ${response.status}.`;
144
+ this.logger.error(errorMessage, line);
145
+ batchResult.errors = [errorMessage];
146
+ }
137
147
  } else {
138
- batchResult.result = response.data.validationMessages.length === 0;
139
- if (!batchResult.result) {
140
- batchResult.failedLines = lines;
148
+ if (response.data.validationMessages.length === 0) {
149
+ batchResult.result = true;
150
+ } else {
151
+ batchResult.result = false;
141
152
  batchResult.errors = response.data.validationMessages.map(
142
- ({description}) => description);
143
- batchResult.groupedFailed = {[batchResult.errors.join()]: [lines[0]]};
153
+ ({ description }) => description);
144
154
  }
145
155
  }
156
+ if (!batchResult.result) {
157
+ batchResult.failedLines = [line];
158
+ batchResult.groupedFailed = { [batchResult.errors.join()]: [line] };
159
+ }
146
160
  return batchResult;
147
161
  };
148
162
  };