Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better error handling and try/catch #146

Merged
merged 1 commit into from
Oct 31, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions app/requests.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,39 @@ module.exports = {
url: opts.url,
rejectUnauthorized: false, // Don't validate SSL certs
headers: opts.headers || {},
json: true
json: true,
agent: false,
timeout: 0, // avoid timeouts
agentOptions: {
keepAlive: false, // "http.Agent: idle sockets throw unhandled ECONNRESET"
maxSockets: 200 // Infinity switches globalAgent on.
}
},
function (error, response, body) {
if (response.statusCode === 200) {
callback(error, body);
} else {
let httpErrRes = 'HTTP Reponse: '+response.statusCode+' '+http.STATUS_CODES[response.statusCode];
if (error) {
error.message += ' ('+httpErrRes+')';
callback(error);
try {
if (response != undefined && response.statusCode === 200) {
callback(error, body);
} else {
// If the request never reached the server, then chances are the error object is null, so lets return a status code error instead
callback(new Error(httpErrRes));
if (response != undefined)
{
let httpErrRes = 'HTTP Reponse: '+response.statusCode+' '+http.STATUS_CODES[response.statusCode];
if (error) {
error.message += ' ('+httpErrRes+')';
callback(error);
} else {
// If the request never reached the server, then chances are the error object is null, so lets return a status code error instead
callback(new Error(httpErrRes));
}
}
else{
callback(error, body);
}
}
}
catch (err) // some exception cannot be handled, like ECONNRESET
{
callback(error, body);
}
});
}
}
Expand Down