Skip to content

Commit

Permalink
Handle error events (fixes #10)
Browse files Browse the repository at this point in the history
  • Loading branch information
DoctorMcKay committed Nov 7, 2017
1 parent 87a5731 commit a547e47
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ OPSkinsAPI.prototype._req = function(httpMethod, iface, method, version, input,

input = input || {};
var rawInput = input;
var errored = false;

// preprocess arrays
for (var i in input) {
Expand Down Expand Up @@ -94,6 +95,10 @@ OPSkinsAPI.prototype._req = function(httpMethod, iface, method, version, input,
"headers": headers,
"agent": this.getAgent()
}, function(res) {
if (errored) {
return;
}

var err = new Error();
err.httpCode = res.statusCode;

Expand Down Expand Up @@ -125,6 +130,15 @@ OPSkinsAPI.prototype._req = function(httpMethod, iface, method, version, input,
return;
}

res.on('error', function(err) {
if (errored) {
return;
}

errored = true;
callback(err);
});

var response = '';
var stream = res;

Expand All @@ -139,6 +153,12 @@ OPSkinsAPI.prototype._req = function(httpMethod, iface, method, version, input,
});

stream.on('end', function() {
if (errored) {
return;
}

errored = true; // just in case

try {
response = JSON.parse(response);
} catch (e) {
Expand Down Expand Up @@ -189,6 +209,15 @@ OPSkinsAPI.prototype._req = function(httpMethod, iface, method, version, input,

// send the actual request
req.end(httpMethod == "POST" ? input : null);

req.on('error', function(err) {
if (errored) {
return;
}

errored = true;
callback(err);
});
};

OPSkinsAPI.prototype._requireKey = function() {
Expand Down

0 comments on commit a547e47

Please sign in to comment.