@google-cloud/nodejs-common 1.5.5 → 1.5.7-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.5",
3
+ "version": "1.5.7-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,39 +113,62 @@ 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
- }
130
- this.logger.debug('Configuration:', config);
131
- this.logger.debug('Input Data: ', lines);
132
- this.logger.debug(`Batch[${batchId}] status: ${response.status}`);
133
- this.logger.debug(response.data);
134
- // There is not enough information from the non-debug mode.
135
- if (!this.debugMode) {
136
- batchResult.result = true;
137
- } else {
138
- batchResult.result = response.data.validationMessages.length === 0;
139
- if (!batchResult.result) {
140
- batchResult.failedLines = lines;
141
- batchResult.errors = response.data.validationMessages.map(
142
- ({description}) => description);
143
- batchResult.groupedFailed = {[batchResult.errors.join()]: [lines[0]]};
123
+ let retriedTimes = 0;
124
+ let response;
125
+ while (retriedTimes <= RETRY_TIMES) {
126
+ try {
127
+ response = await request(requestOptions);
128
+ if (response.status < 500) break; // Only retry when server errors.
129
+ this.logger.warn('Got a 5XX error', response);
130
+ } catch (error) {
131
+ this.logger.warn('Got an error', error);
132
+ if (retriedTimes === RETRY_TIMES) {
133
+ this.logger.error('Maximum retry times exceeded.');
134
+ batchResult.result = false;
135
+ batchResult.errors = [error.toString()];
136
+ break;
137
+ }
144
138
  }
139
+ retriedTimes++;
140
+ await wait(retriedTimes * 1000);
141
+ this.logger.warn('Will retry now...');
142
+ };
143
+ if (response) {
144
+ this.logger.debug('Configuration:', config);
145
+ this.logger.debug('Input Data: ', lines);
146
+ this.logger.debug(`Batch[${batchId}] status: ${response.status}`);
147
+ this.logger.debug(response.data);
148
+ // There is not enough information from the non-debug mode.
149
+ if (!this.debugMode) {
150
+ if (response.status >= 200 && response.status < 300) {
151
+ batchResult.result = true;
152
+ } else {
153
+ batchResult.result = false;
154
+ const errorMessage =
155
+ `MP GA4 [${batchId}] http status ${response.status}.`;
156
+ this.logger.error(errorMessage, line);
157
+ batchResult.errors = [errorMessage];
158
+ }
159
+ } else {
160
+ if (response.data.validationMessages.length === 0) {
161
+ batchResult.result = true;
162
+ } else {
163
+ batchResult.result = false;
164
+ batchResult.errors = response.data.validationMessages.map(
165
+ ({ description }) => description);
166
+ }
167
+ }
168
+ }
169
+ if (!batchResult.result) {
170
+ batchResult.failedLines = [line];
171
+ batchResult.groupedFailed = { [batchResult.errors.join()]: [line] };
145
172
  }
146
173
  return batchResult;
147
174
  };