Skip to content

Commit

Permalink
Merge pull request #51 from saksham-gg/master
Browse files Browse the repository at this point in the history
FORCE-3955 error handling improvements
  • Loading branch information
Shahnawaz-Sk authored May 10, 2024
2 parents 9f71d7a + 0bd6193 commit fa18cb0
Show file tree
Hide file tree
Showing 4 changed files with 803 additions and 3,288 deletions.
5 changes: 3 additions & 2 deletions lib/cfg/node-tunnel-config-v3-latest.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
},
"AuthUrl": "https://accounts.lambdatest.com/api/user/token/auth",
"logEnable": true,
"latest": "4.0.7",
"latest": "4.0.8",
"supportedVersions":
[
"1.0.0",
Expand Down Expand Up @@ -120,6 +120,7 @@
"4.0.4",
"4.0.5",
"4.0.6",
"4.0.7"
"4.0.7",
"4.0.8"
]
}
153 changes: 100 additions & 53 deletions lib/tunnel.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,81 +412,128 @@ function runBinary_(self, retries, fnCallback) {
}

/**
* verifyToken_ is used to Run Tunnel Binary
* @param {Setting} options passed User argumants.
* verifyToken_ is used to verify user credentials by sending a POST request to the authentication server with retries and exponential backoff.
* @param {Setting} options passed User arguments.
* @param {Function} fnCallback is a Callable function.
* @return {Object|Error} Return User Object or Error if any.
*/
function verifyToken_(options, fnCallback) {
try {
var data = JSON.stringify({
username: options['user'],
token: options['key']
});
function verifyToken_(options, fnCallback, retries = 3, delay = 1000) {
const attempt = () => {
try {
var data = JSON.stringify({
username: options['user'],
token: options['key']
});

var _httpAuthUrl = urlParse.parse(httpTunnelConfig.jsonResponse.AuthUrl);
var reqOptions = {
hostname: _httpAuthUrl.hostname,
port: _httpAuthUrl.port,
path: _httpAuthUrl.path,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length,
Accept: 'application/json',
client: 'npm-tunnel',
version: packageVersion
}
};

var _httpAuthUrl = urlParse.parse(httpTunnelConfig.jsonResponse.AuthUrl);
var reqOptions = {
hostname: _httpAuthUrl.hostname,
port: _httpAuthUrl.port,
path: _httpAuthUrl.path,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length,
Accept: 'application/json',
client: 'npm-tunnel',
version: packageVersion
var proxyOpts = util.getProxyOpts_(options);
if (Object.keys(proxyOpts).length) {
reqOptions.agent = new HttpsProxyAgent(proxyOpts);
}
};
var proxyOpts = util.getProxyOpts_(options);
if (Object.keys(proxyOpts).length) {
reqOptions.agent = new HttpsProxyAgent(proxyOpts);
}
var req = https.request(reqOptions, resp => {
let json = '';
resp.on('data', chunk => {
json += chunk;

var req = https.request(reqOptions, resp => {
let json = '';
resp.on('data', chunk => {
json += chunk;
});
resp.on('end', () => {
try {
if (resp.statusCode >= 500) {
throw new Error(`Server error with status code: ${resp.statusCode}`);
}
if (typeof json === 'string') {
json = JSON.parse(json);
}
if (json && json.type === 'error') {
logger.log(
options['user'],
options['key'],
{ filename: __filename },
options,
'Authentication failed: ' + json.message
);
throw new Error(json.message);
}
return fnCallback(false, json);
} catch (parseError) {
if (retries > 0) {
logger.log(
options['user'],
options['key'],
{ filename: __filename },
options,
`Parse or server error on attempt ${4 - retries}, retrying... Error: ` + parseError
);
setTimeout(() => {
verifyToken_(options, fnCallback, retries - 1, delay * 2);
}, delay);
} else {
logger.log(
options['user'],
options['key'],
{ filename: __filename },
options,
'Parse or server error, maximum retries reached. Error: ' + parseError
);
return fnCallback(true, parseError);
}
}
});
});
resp.on('end', () => {
if (typeof json === 'string') {
json = JSON.parse(json);
}
if (json && json.type === 'error') {

req.on('error', e => {
if (e.code === 'ECONNRESET' || e.code === 'ENOTFOUND' || e.code === 'ETIMEDOUT' || retries > 0) {
logger.log(
options['user'],
options['key'],
{ filename: __filename },
options,
'Getting this issue while user is not valid' + json
`Network error on attempt ${4 - retries}, retrying... Error: ` + e
);
return fnCallback(true, json);
setTimeout(() => {
verifyToken_(options, fnCallback, retries - 1, delay * 2);
}, delay);
} else {
logger.log(
options['user'],
options['key'],
{ filename: __filename },
options,
'Network error, maximum retries reached. Error: ' + e
);
return fnCallback(true, e);
}
return fnCallback(false, json);
});
});

req.on('error', e => {
req.write(data);
req.end();
} catch (e) {
logger.log(
options['user'],
options['key'],
{ filename: __filename },
options,
'We are getting this error while verifying user and key validity. Error : ' + e
'Something unexpected while setting up the request. Error : ' + e
);
return fnCallback(true, e);
});
req.write(data);
req.end();
} catch (e) {
logger.log(
options['user'],
options['key'],
{ filename: __filename },
options,
'Something unexpected while verifying the user and key validity. Error : ' + e
);
return fnCallback(true, e);
}
}
};

attempt();
}

/**
Expand Down
Loading

0 comments on commit fa18cb0

Please sign in to comment.