diff --git a/src/routes/apps.js b/src/routes/apps.js index 2517d6f79..43736f70e 100644 --- a/src/routes/apps.js +++ b/src/routes/apps.js @@ -460,7 +460,7 @@ function exec(req, res, next) { }); } -function execWebSocket(ws, req) { +function execWebSocket(ws, req, next) { assert.strictEqual(typeof req.params.id, 'string'); debug('Execing into app id:%s and cmd:%s', req.params.id, req.query.cmd); @@ -468,35 +468,41 @@ function execWebSocket(ws, req) { var cmd = null; if (req.query.cmd) { cmd = safe.JSON.parse(req.query.cmd); - if (!util.isArray(cmd) || cmd.length < 1) return console.error(new HttpError(400, 'cmd must be array with atleast size 1')); + if (!util.isArray(cmd) || cmd.length < 1) return next(new HttpError(400, 'cmd must be array with atleast size 1')); } var columns = req.query.columns ? parseInt(req.query.columns, 10) : null; - if (isNaN(columns)) return console.error(new HttpError(400, 'columns must be a number')); + if (isNaN(columns)) return next(new HttpError(400, 'columns must be a number')); var rows = req.query.rows ? parseInt(req.query.rows, 10) : null; - if (isNaN(rows)) return console.error(new HttpError(400, 'rows must be a number')); + if (isNaN(rows)) return next(new HttpError(400, 'rows must be a number')); var tty = req.query.tty === 'true' ? true : false; // req.clearTimeout(); apps.exec(req.params.id, { cmd: cmd, rows: rows, columns: columns, tty: tty }, function (error, duplexStream) { - if (error && error.reason === AppsError.NOT_FOUND) return console.error(new HttpError(404, 'No such app')); - if (error && error.reason === AppsError.BAD_STATE) return console.error(new HttpError(409, error.message)); - if (error) return console.error(new HttpError(500, error)); + if (error && error.reason === AppsError.NOT_FOUND) return next(new HttpError(404, 'No such app')); + if (error && error.reason === AppsError.BAD_STATE) return next(new HttpError(409, error.message)); + if (error) return next(new HttpError(500, error)); console.log('Connected to terminal'); - + duplexStream.on('end', function () { ws.end(); }); + duplexStream.on('close', function () { ws.end(); }); + duplexStream.on('error', function (error) { + console.error('duplexStream error:', error); + }); duplexStream.on('data', function (data) { ws.send(data.toString()); }); + ws.on('error', function (error) { + console.error('websocket error:', error); + }); ws.on('message', function (msg) { duplexStream.write(msg); }); - ws.on('close', function () { // Clean things up, if any? });