Skip to content

Commit

Permalink
fix(xhrUtil): Added better logging and retries in XHRUtil
Browse files Browse the repository at this point in the history
  • Loading branch information
rohitesh-wingify committed Jan 2, 2024
1 parent aa32575 commit 74daacc
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 45 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.64.3] - 2024-01-02

### Added

- Added better logging and retries in XHRUtil

## [1.64.2] - 2023-12-19

### Fixed
Expand Down
89 changes: 65 additions & 24 deletions dist/vwo-javascript-sdk.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/vwo-javascript-sdk.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/vwo-javascript-sdk.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/vwo-javascript-sdk.min.js.map

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion lib/utils/EventDispatcherUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ let EventDispatcher = {
method: 'POST',
url: `${properties.url}${queryParams}`,
payload,
customHeaders
customHeaders,
logger
})
.then(() => {
this.handlePostResponse(properties, payload);
Expand Down
85 changes: 68 additions & 17 deletions lib/utils/XhrUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

const logging = require('../services/logging');
const { LogLevelEnum } = logging;
const { getCurrentTime } = require('./FunctionUtil');
const { isObject, isFunction } = require('./DataTypeUtil');

Expand Down Expand Up @@ -55,7 +57,7 @@ const XhrUtil = {
parsedSettings
};
},
send: function({ method, url, payload, userStorageService, customHeaders } = {}) {
send: function({ method, url, payload, userStorageService, customHeaders, logger } = {}) {
if (!url || !method) {
return;
}
Expand All @@ -67,12 +69,68 @@ const XhrUtil = {
resolve(parsedSettings);
} else {
let xhr = new XMLHttpRequest();
this.xhrHandler(xhr, method, url, payload, userStorageService, customHeaders, resolve, reject);
this.xhrHandler(xhr, method, url, payload, userStorageService, customHeaders, logger, resolve, reject);
}
});
},

xhrHandler: function(xhr, method, url, payload, userStorageService, customHeaders = {}, resolve, reject) {
// send request function definition (to allow for retries)
sendRequest: function(xhr, retries, maxRetries, delay, logger, customHeaders, payload, method, url, resolve, reject) {
// onload event
xhr.onload = () => {
// retry if error and less than max retries
if (xhr.status < 200 || xhr.status >= 300) {
if (retries < maxRetries) {
retries++;

// log retried times
logger.log(LogLevelEnum.ERROR, 'Retrying with Status Code :' + xhr.status);
logger.log(LogLevelEnum.ERROR, 'Retrying with Response :' + xhr.responseText);

// call send request again, after delay
setTimeout(() => {
this.sendRequest(
xhr,
retries,
maxRetries,
delay,
logger,
customHeaders,
payload,
method,
url,
resolve,
reject
);
}, delay);
} else {
// log errors with status (clean up later)
logger.log(LogLevelEnum.ERROR, 'Request failed with Status Code :' + xhr.status);
logger.log(LogLevelEnum.ERROR, 'Request failed with Response :' + xhr.responseText);
reject(`Got Error: ${xhr.statusText} and Status Code: ${xhr.status}`);
}
} else {
// resolve the promise if all well
resolve();
}
};

// onerror event
xhr.onerror = () => {
reject(`Error: ${xhr.statusText}, Status Code: ${xhr.status}`);
};

// open connection and add headers if any, and then send
xhr.open(method, url, true);
for (var newHeaderName in customHeaders) {
if (customHeaders.hasOwnProperty(newHeaderName)) {
xhr.setRequestHeader(newHeaderName, customHeaders[newHeaderName]);
}
}
xhr.send(JSON.stringify(payload));
},

xhrHandler: function(xhr, method, url, payload, userStorageService, customHeaders = {}, logger, resolve, reject) {
if (method === 'GET') {
try {
xhr.onload = () => {
Expand All @@ -93,20 +151,13 @@ const XhrUtil = {
console.log(e.message);
}
} else if (method === 'POST') {
xhr.onload = () => {
resolve();
};
xhr.onerror = () => {
reject(`Error: ${xhr.statusText}, Status Code: ${xhr.status}`);
};

xhr.open(method, url, true);
for (var newHeaderName in customHeaders) {
if (customHeaders.hasOwnProperty(newHeaderName)) {
xhr.setRequestHeader(newHeaderName, customHeaders[newHeaderName]);
}
}
xhr.send(JSON.stringify(payload));
// retry params
let retries = 0;
let maxRetries = 5;
let delay = 1000;

// send request
this.sendRequest(xhr, retries, maxRetries, delay, logger, customHeaders, payload, method, url, resolve, reject);
}
},

Expand Down

0 comments on commit 74daacc

Please sign in to comment.