Skip to content

Commit

Permalink
Fix error handling in https.request function This commit addresses an…
Browse files Browse the repository at this point in the history
… issue with error handling in the https.request function. Previously, exceptions were not properly caught and handled, resulting in unexpected behaviour. The fix includes implementing proper error handling logic. Now, network errors, invalid URLs, and SSL/TLS certificate errors are handled gracefully, improving the reliability of the function.
  • Loading branch information
malsabbagh committed May 25, 2023
1 parent 9130189 commit 3967cba
Showing 1 changed file with 27 additions and 23 deletions.
50 changes: 27 additions & 23 deletions lib/webhelper.js
Expand Up @@ -126,32 +126,36 @@ function WebHelper(credentials, hostName) {
addUserAgentString(options);

return new Promise((resolve, reject) => {
var request = https.request(options, function (response) {

response.setEncoding("utf8");
var str = "";

response.on("data", function (chunk) {
str += chunk;
});
response.on("end", function () {
if (response.statusCode >= 400) {
var err = new Error(response.statusMessage);
err.statusCode = response.statusCode;
try {
var request = https.request(options, function (response) {

response.setEncoding("utf8");
var str = "";

response.on("data", function (chunk) {
str += chunk;
});
response.on("end", function () {
if (response.statusCode >= 400) {
var err = new Error(response.statusMessage);
err.statusCode = response.statusCode;
reject(err);

} else {
resolve((str.length > 0) ? JSON.parse(str) : null);
}
});
response.on("error", function (err) {
reject(err);

} else {
resolve((str.length > 0) ? JSON.parse(str) : null);
}
});
response.on("error", function (err) {
reject(err);
});
});
});
if (postData) {
request.write(postData);
if (postData) {
request.write(postData);
}
request.end();
} catch (err) {
reject(err);
}
request.end();
});
}

Expand Down

0 comments on commit 3967cba

Please sign in to comment.