diff --git a/mysql.js b/mysql.js index 763973a..87d086c 100644 --- a/mysql.js +++ b/mysql.js @@ -3,8 +3,8 @@ * @author David Spreekmeester */ -const mysql = require('mysql2') -const Client = require('ssh2').Client; +const mysql = require('mysql2') +const Client = require('ssh2').Client; var tunnel = module.exports = { @@ -23,11 +23,11 @@ var tunnel = module.exports = { * @param obj dbConfig MySQL Configuration as defined by mysql(2) package * @return Promise */ - connect: function(sshConfig, dbConfig) { + connect: function (sshConfig, dbConfig) { dbConfig = tunnel._addDefaults(dbConfig) - return new Promise(function(resolve, reject) { + return new Promise(function (resolve, reject) { tunnel._conn = new Client(); - tunnel._conn.on('ready', function() { + tunnel._conn.on('ready', function () { tunnel._conn.forwardOut( '127.0.0.1', 12345, @@ -54,15 +54,25 @@ var tunnel = module.exports = { }) }, - close: function() { - if ('end' in tunnel._sql) { - tunnel._sql.end(function(err) {}) - } + close: function () { + try { + if ('end' in tunnel._sql) { + tunnel._sql.end(function (err) { + if (err) { throw new Error(err); } + }) + } + } catch (error) { + console.log(`error closing mysql connection: ${error.message}`); + } - if ('end' in tunnel._conn) { - tunnel._conn.end() - } - }, + try { + if ('end' in tunnel._conn) { + tunnel._conn.end() + } + } catch (error) { + console.log(`error closing ssh connection: ${error.message}`); + } + }, _addDefaults(dbConfig) { if (!('port' in dbConfig)) { diff --git a/package.json b/package.json index 5012562..ecc1e8f 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "ssh2": "^0.5.4" }, "name": "mysql-ssh", - "version": "1.0.6", + "version": "1.0.7", "description": "Execute mysql commands over an ssh tunnel.", "main": "mysql.js", "devDependencies": {},