From 7330db35631a5510af438f2793ce71df00c0f42f Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Fri, 24 Mar 2023 16:08:30 +0800 Subject: [PATCH 1/2] Improve error handling of mysqlQuery and return row count as result --- server/model/monitor.js | 4 +--- server/util-server.js | 29 +++++++++++++++++++---------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/server/model/monitor.js b/server/model/monitor.js index 1e011c5a0..1ffae58e5 100644 --- a/server/model/monitor.js +++ b/server/model/monitor.js @@ -637,9 +637,7 @@ class Monitor extends BeanModel { } else if (this.type === "mysql") { let startTime = dayjs().valueOf(); - await mysqlQuery(this.databaseConnectionString, this.databaseQuery); - - bean.msg = ""; + bean.msg = await mysqlQuery(this.databaseConnectionString, this.databaseQuery); bean.status = UP; bean.ping = dayjs().valueOf() - startTime; } else if (this.type === "mongodb") { diff --git a/server/util-server.js b/server/util-server.js index 01e66ee26..1cdf33de5 100644 --- a/server/util-server.js +++ b/server/util-server.js @@ -322,21 +322,30 @@ exports.postgresQuery = function (connectionString, query) { * Run a query on MySQL/MariaDB * @param {string} connectionString The database connection string * @param {string} query The query to validate the database with - * @returns {Promise<(string[]|Object[]|Object)>} + * @returns {Promise<(string)>} */ exports.mysqlQuery = function (connectionString, query) { return new Promise((resolve, reject) => { const connection = mysql.createConnection(connectionString); - connection.promise().query(query) - .then(res => { - resolve(res); - }) - .catch(err => { + + connection.on("error", (err) => { + reject(err); + }); + + connection.query(query, (err, res) => { + if (err) { reject(err); - }) - .finally(() => { - connection.destroy(); - }); + } + + // Check if res is an array + if (Array.isArray(res)) { + resolve("Rows: " + res.length); + } else { + resolve("No Error, but the result is not an array. Type: " + typeof res); + } + + connection.destroy(); + }); }); }; From f4ee5271afeb44cac945d0f07e9bc0413956ee19 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Fri, 24 Mar 2023 16:24:00 +0800 Subject: [PATCH 2/2] Improve error handling of mysqlQuery and return row count as result --- server/util-server.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/server/util-server.js b/server/util-server.js index 1cdf33de5..47f6bb48c 100644 --- a/server/util-server.js +++ b/server/util-server.js @@ -335,15 +335,13 @@ exports.mysqlQuery = function (connectionString, query) { connection.query(query, (err, res) => { if (err) { reject(err); - } - - // Check if res is an array - if (Array.isArray(res)) { - resolve("Rows: " + res.length); } else { - resolve("No Error, but the result is not an array. Type: " + typeof res); + if (Array.isArray(res)) { + resolve("Rows: " + res.length); + } else { + resolve("No Error, but the result is not an array. Type: " + typeof res); + } } - connection.destroy(); }); });