implemented sending of errors not directly from requests

This commit is contained in:
tigerbot 2017-04-28 14:19:56 -06:00
parent b4a300cc64
commit 3d5f4a773d
1 changed files with 9 additions and 2 deletions

View File

@ -182,7 +182,7 @@ module.exports.create = function (copts) {
if (firstToken) {
var err = addToken(firstToken);
if (err) {
ws.send(JSON.stringify({ error: err }));
ws.send(packer.pack(null, [0, err], 'control'));
ws.close();
return;
}
@ -210,7 +210,9 @@ module.exports.create = function (copts) {
cmd = JSON.parse(opts.data.toString());
} catch (err) {}
if (!Array.isArray(cmd) || typeof cmd[0] !== 'number') {
console.warn('received bad command "' + opts.data.toString() + '"');
var msg = 'received bad command "' + opts.data.toString() + '"';
console.warn(msg, 'from websocket', socketId);
ws.send(packer.pack(null, [0, {message: msg, code: 'E_BAD_COMMAND'}], 'control'));
return;
}
@ -227,6 +229,11 @@ module.exports.create = function (copts) {
return;
}
if (cmd[0] === 0) {
console.warn('received dis-associated error from', socketId, cmd[1]);
return;
}
if (commandHandlers[cmd[1]]) {
err = commandHandlers[cmd[1]].apply(null, cmd.slice(2));
}