Skip to content

Commit

Permalink
Improving error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
madpilot committed Dec 7, 2016
1 parent 6684bc8 commit 61f9ba8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
4 changes: 2 additions & 2 deletions software/devices/lirc.js
Expand Up @@ -82,11 +82,11 @@ LIRCDevice.prototype.sendStop = function(key) {
});
}

LIRCDevice.prototype.status = function(status) {
LIRCDevice.prototype.status = function(status, cb) {
cb("Not supported");
}

LIRCDevice.prototype.statuses = function(status) {
LIRCDevice.prototype.statuses = function(cb) {
cb(null, []);
}

Expand Down
2 changes: 1 addition & 1 deletion software/index.js
Expand Up @@ -77,7 +77,7 @@ function handler(device, event, key, cb) {
break;
case 'statuses':
logger.info("Requesting list of supported statuses from " + device);
devices[devices].statuses(cb);
devices[device].statuses(cb);
break;
}
}
Expand Down
38 changes: 31 additions & 7 deletions software/inputs/http.js
Expand Up @@ -18,8 +18,8 @@ function setHeaders(res) {
res.setHeader('Content-Type', 'application/json');
}

function returnError(res, error) {
logger.error("[HTTP Input] Error sendingStop: " + error);
function returnError(req, res, error) {
logger.error("[HTTP Input] " + req.method + " " + req.url + " " + error);
res.status(500);
res.send(JSON.stringify({ error: error }));
}
Expand All @@ -30,7 +30,7 @@ HttpInput.prototype.listen = function(handler) {
handler(null, null, null, function(error, ids) {
setHeaders(res);
if(error != null) {
returnError(res, error);
returnError(req, res, error);
} else {
res.send(JSON.stringify(ids));
}
Expand All @@ -41,7 +41,7 @@ HttpInput.prototype.listen = function(handler) {
handler(req.params.device, "list", null, function(error, keys) {
setHeaders(res);
if(error != null) {
returnError(res, error);
returnError(req, res, error);
} else {
res.send(JSON.stringify(keys));
}
Expand All @@ -52,7 +52,7 @@ HttpInput.prototype.listen = function(handler) {
handler(req.params.device, "sendOnce", req.body.key, function(error, success) {
setHeaders(res);
if(error != null) {
returnError(res, error);
returnError(req, res, error);
} else {
res.send(JSON.stringify({ status: "OK" }));
}
Expand All @@ -63,7 +63,7 @@ HttpInput.prototype.listen = function(handler) {
handler(req.params.device, "sendStart", req.params.key, function(error, success) {
setHeaders(res);
if(error != null) {
returnError(res, error);
returnError(req, res, error);
} else {
res.send(JSON.stringify({ status: "OK" }));
}
Expand All @@ -74,13 +74,37 @@ HttpInput.prototype.listen = function(handler) {
handler(req.params.device, "sendStop", req.params.key, function(error, success) {
setHeaders(res);
if(error != null) {
returnError(res, error);
returnError(req, res, error);
} else {
res.send(JSON.stringify({ status: "OK" }));
}
});
});

app.get("/status/:device", function(req, res) {
handler(req.params.device, "status", req.params.key, function(error, status) {
setHeaders(res);
if(error != null) {
returnError(req, res, error);
} else {
res.send(JSON.stringify(status));
}
});
});

app.get("/statuses/:device", function(req, res) {
handler(req.params.device, "statuses", null, function(error, statuses) {
setHeaders(res);
if(error != null) {
returnError(req, res, error);
} else {
res.send(JSON.stringify(statuses));
}
});
});



app.listen(this.port, function() {
logger.info("[HTTP Input] Listening on " + context.bind + ":" + context.port);
});
Expand Down

0 comments on commit 61f9ba8

Please sign in to comment.