Skip to content

Commit

Permalink
Merge pull request #146 from Alea81/feature/error_handling
Browse files Browse the repository at this point in the history
Better error handling and try/catch
  • Loading branch information
marcells committed Oct 31, 2018
2 parents 66630e4 + 3726a97 commit aaf1efe
Showing 1 changed file with 28 additions and 10 deletions.
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

0 comments on commit aaf1efe

Please sign in to comment.