diff --git a/src/main/libs/clients/BaseClient.ts b/src/main/libs/clients/BaseClient.ts index 0c79e5d5..244b4ed3 100644 --- a/src/main/libs/clients/BaseClient.ts +++ b/src/main/libs/clients/BaseClient.ts @@ -3,14 +3,25 @@ import mysql from 'mysql2/promise'; import * as pg from 'pg'; import SSH2Promise = require('@fabio286/ssh2-promise'); -const queryLogger = ({ sql, cUid }: {sql: string; cUid: string}) => { - // Remove comments, newlines and multiple spaces - const escapedSql = sql.replace(/(\/\*(.|[\r\n])*?\*\/)|(--(.*|[\r\n]))/gm, '').replace(/\s\s+/g, ' '); - if (process.type !== undefined) { - const mainWindow = require('electron').webContents.fromId(1); - mainWindow.send('query-log', { cUid, sql: escapedSql, date: new Date() }); +export type LoggerLevel = 'query' | 'error' + +const ipcLogger = ({ content, cUid, level }: {content: string; cUid: string; level: LoggerLevel}) => { + if (level === 'error') { + if (process.type !== undefined) { + const mainWindow = require('electron').webContents.fromId(1); + mainWindow.send('non-blocking-exception', { cUid, message: content, date: new Date() }); + } + if (process.env.NODE_ENV === 'development' && process.type === 'browser') console.log(content); + } + else if (level === 'query') { + // Remove comments, newlines and multiple spaces + const escapedSql = content.replace(/(\/\*(.|[\r\n])*?\*\/)|(--(.*|[\r\n]))/gm, '').replace(/\s\s+/g, ' '); + if (process.type !== undefined) { + const mainWindow = require('electron').webContents.fromId(1); + mainWindow.send('query-log', { cUid, sql: escapedSql, date: new Date() }); + } + if (process.env.NODE_ENV === 'development' && process.type === 'browser') console.log(escapedSql); } - if (process.env.NODE_ENV === 'development' && process.type === 'browser') console.log(escapedSql); }; /** @@ -22,7 +33,7 @@ export abstract class BaseClient { protected _params: mysql.ConnectionOptions | pg.ClientConfig | { databasePath: string; readonly: boolean}; protected _poolSize: number; protected _ssh?: SSH2Promise; - protected _logger: (args: {sql: string; cUid: string}) => void; + protected _logger: (args: {content: string; cUid: string; level: LoggerLevel}) => void; protected _queryDefaults: antares.QueryBuilderObject; protected _query: antares.QueryBuilderObject; @@ -31,7 +42,7 @@ export abstract class BaseClient { this._cUid = args.uid; this._params = args.params; this._poolSize = args.poolSize || undefined; - this._logger = args.logger || queryLogger; + this._logger = args.logger || ipcLogger; this._queryDefaults = { schema: '', diff --git a/src/main/libs/clients/FirebirdSQLClient.ts b/src/main/libs/clients/FirebirdSQLClient.ts index 33a0dc1f..181d7e74 100644 --- a/src/main/libs/clients/FirebirdSQLClient.ts +++ b/src/main/libs/clients/FirebirdSQLClient.ts @@ -1024,7 +1024,7 @@ export class FirebirdSQLClient extends BaseClient { alias: string; } - this._logger({ cUid: this._cUid, sql }); + this._logger({ cUid: this._cUid, content: sql, level: 'query' }); args = { nest: false, diff --git a/src/main/libs/clients/MySQLClient.ts b/src/main/libs/clients/MySQLClient.ts index f9733e4b..6f99f19b 100644 --- a/src/main/libs/clients/MySQLClient.ts +++ b/src/main/libs/clients/MySQLClient.ts @@ -354,10 +354,21 @@ export class MySQLClient extends BaseClient { if (this._params.schema) filteredDatabases = filteredDatabases.filter(db => db.Database === this._params.schema); - const { rows: functions } = await this.raw('SHOW FUNCTION STATUS'); - const { rows: procedures } = await this.raw('SHOW PROCEDURE STATUS'); - // eslint-disable-next-line @typescript-eslint/no-explicit-any + /* eslint-disable @typescript-eslint/no-explicit-any */ + let functions: any[] = []; + let procedures: any[] = []; let schedulers: any[] = []; + /* eslint-enable @typescript-eslint/no-explicit-any */ + + try { + const { rows: functionRows } = await this.raw('SHOW FUNCTION STATUS'); + const { rows: procedureRows } = await this.raw('SHOW PROCEDURE STATUS'); + functions = functionRows; + procedures = procedureRows; + } + catch (err) { + this._logger({ content: err.sqlMessage, cUid: this._cUid, level: 'error' }); + } try { // Avoid exception with event_scheduler DISABLED with MariaDB 10 const { rows } = await this.raw('SELECT *, EVENT_SCHEMA AS `Db`, EVENT_NAME AS `Name` FROM information_schema.`EVENTS`'); @@ -1667,7 +1678,7 @@ export class MySQLClient extends BaseClient { } async raw (sql: string, args?: antares.QueryParams) { - this._logger({ cUid: this._cUid, sql }); + this._logger({ cUid: this._cUid, content: sql, level: 'query' }); args = { nest: false, diff --git a/src/main/libs/clients/PostgreSQLClient.ts b/src/main/libs/clients/PostgreSQLClient.ts index 5b7deeaa..08ffd238 100644 --- a/src/main/libs/clients/PostgreSQLClient.ts +++ b/src/main/libs/clients/PostgreSQLClient.ts @@ -1645,7 +1645,7 @@ export class PostgreSQLClient extends BaseClient { } async raw (sql: string, args?: antares.QueryParams) { - this._logger({ cUid: this._cUid, sql }); + this._logger({ cUid: this._cUid, content: sql, level: 'query' }); args = { nest: false, diff --git a/src/main/libs/clients/SQLiteClient.ts b/src/main/libs/clients/SQLiteClient.ts index e9511218..73969085 100644 --- a/src/main/libs/clients/SQLiteClient.ts +++ b/src/main/libs/clients/SQLiteClient.ts @@ -612,7 +612,7 @@ export class SQLiteClient extends BaseClient { } async raw (sql: string, args?: antares.QueryParams) { - this._logger({ cUid: this._cUid, sql });// TODO: replace BLOB content with a placeholder + this._logger({ cUid: this._cUid, content: sql, level: 'query' });// TODO: replace BLOB content with a placeholder args = { nest: false, diff --git a/src/renderer/index.ts b/src/renderer/index.ts index b4d36e3e..3d0a62e1 100644 --- a/src/renderer/index.ts +++ b/src/renderer/index.ts @@ -44,6 +44,15 @@ ipcRenderer.on('unhandled-exception', (event, error) => { date: new Date() }); }); +ipcRenderer.on('non-blocking-exception', (event, error) => { + useNotificationsStore().addNotification({ status: 'error', message: error.message }); + useConsoleStore().putLog('debug', { + level: 'error', + process: 'main', + message: error.message, + date: new Date() + }); +}); // IPC query logs ipcRenderer.on('query-log', (event, logRecord: QueryLog) => {