mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-03 14:38:50 +00:00
refactor(logger): more consistent error output
This commit is contained in:
1 parent
77251b3739
commit
e07947d3b9
4 files changed
+39
-28
No files matched your search
@@ -1,5 +1,5 @@
|
|||||||
import { pool } from '.';
|
import { pool } from '.';
|
||||||
import { profileBatchStatements, runProfiler } from '../logger';
|
import { printError, profileBatchStatements, runProfiler } from '../logger';
|
||||||
import { CFXCallback, CFXParameters } from '../types';
|
import { CFXCallback, CFXParameters } from '../types';
|
||||||
import { parseResponse } from '../utils/parseResponse';
|
import { parseResponse } from '../utils/parseResponse';
|
||||||
import { executeType, parseExecute } from '../utils/parseExecute';
|
import { executeType, parseExecute } from '../utils/parseExecute';
|
||||||
@@ -15,8 +15,12 @@ export const rawExecute = async (
|
|||||||
unpack?: boolean
|
unpack?: boolean
|
||||||
) => {
|
) => {
|
||||||
if (typeof query !== 'string')
|
if (typeof query !== 'string')
|
||||||
throw new Error(
|
return printError(
|
||||||
`${invokingResource} was unable to execute a query!\nExpected query to be a string but received ${typeof query} instead.`
|
invokingResource,
|
||||||
|
cb,
|
||||||
|
isPromise,
|
||||||
|
query,
|
||||||
|
`Expected query to be a string but received ${typeof query} instead.`
|
||||||
);
|
);
|
||||||
|
|
||||||
const type = executeType(query);
|
const type = executeType(query);
|
||||||
@@ -81,18 +85,15 @@ export const rawExecute = async (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
const error = `${invokingResource} was unable to execute a query!\n${err}\n${`${query}`}`;
|
printError(invokingResource, cb, isPromise, query, err.message);
|
||||||
|
|
||||||
TriggerEvent('oxmysql:error', {
|
TriggerEvent('oxmysql:error', {
|
||||||
query: query,
|
query: query,
|
||||||
parameters: parameters,
|
parameters: parameters,
|
||||||
message: (err).message,
|
message: err.message,
|
||||||
err: (err),
|
err: err,
|
||||||
resource: invokingResource,
|
resource: invokingResource,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (cb && isPromise) return cb(null, error);
|
|
||||||
console.error(error);
|
|
||||||
} finally {
|
} finally {
|
||||||
connection.release();
|
connection.release();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { pool, isServerConnected, waitForConnection } from '.';
|
import { pool, isServerConnected, waitForConnection } from '.';
|
||||||
import { parseArguments } from '../utils/parseArguments';
|
import { parseArguments } from '../utils/parseArguments';
|
||||||
import { parseResponse } from '../utils/parseResponse';
|
import { parseResponse } from '../utils/parseResponse';
|
||||||
import { logQuery, runProfiler } from '../logger';
|
import { logQuery, printError, runProfiler } from '../logger';
|
||||||
import type { CFXCallback, CFXParameters } from '../types';
|
import type { CFXCallback, CFXParameters } from '../types';
|
||||||
import type { QueryType } from '../types';
|
import type { QueryType } from '../types';
|
||||||
import { scheduleTick } from '../utils/scheduleTick';
|
import { scheduleTick } from '../utils/scheduleTick';
|
||||||
@@ -16,8 +16,12 @@ export const rawQuery = async (
|
|||||||
isPromise?: boolean
|
isPromise?: boolean
|
||||||
) => {
|
) => {
|
||||||
if (typeof query !== 'string')
|
if (typeof query !== 'string')
|
||||||
throw new Error(
|
return printError(
|
||||||
`${invokingResource} was unable to execute a query!\nExpected query to be a string but received ${typeof query} instead.`
|
invokingResource,
|
||||||
|
cb,
|
||||||
|
isPromise,
|
||||||
|
query,
|
||||||
|
`Expected query to be a string but received ${typeof query} instead.`
|
||||||
);
|
);
|
||||||
|
|
||||||
[query, parameters, cb] = parseArguments(invokingResource, query, parameters, cb);
|
[query, parameters, cb] = parseArguments(invokingResource, query, parameters, cb);
|
||||||
@@ -50,9 +54,7 @@ export const rawQuery = async (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
const error = `${invokingResource} was unable to execute a query!\n${err.message}\n${`${query} ${JSON.stringify(
|
printError(invokingResource, cb, isPromise, query, JSON.stringify(parameters), err.message);
|
||||||
parameters
|
|
||||||
)}`}`;
|
|
||||||
|
|
||||||
TriggerEvent('oxmysql:error', {
|
TriggerEvent('oxmysql:error', {
|
||||||
query: query,
|
query: query,
|
||||||
@@ -61,9 +63,6 @@ export const rawQuery = async (
|
|||||||
err: err,
|
err: err,
|
||||||
resource: invokingResource,
|
resource: invokingResource,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (cb && isPromise) return cb(null, error);
|
|
||||||
console.error(error);
|
|
||||||
} finally {
|
} finally {
|
||||||
connection.release();
|
connection.release();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import { pool, isServerConnected, waitForConnection } from '.';
|
import { pool, isServerConnected, waitForConnection } from '.';
|
||||||
import { profileBatchStatements, runProfiler } from '../logger';
|
import { printError, profileBatchStatements, runProfiler } from '../logger';
|
||||||
import { CFXCallback, CFXParameters, TransactionQuery } from '../types';
|
import { CFXCallback, CFXParameters, TransactionQuery } from '../types';
|
||||||
import { parseTransaction } from '../utils/parseTransaction';
|
import { parseTransaction } from '../utils/parseTransaction';
|
||||||
import { scheduleTick } from '../utils/scheduleTick';
|
import { scheduleTick } from '../utils/scheduleTick';
|
||||||
@@ -43,20 +43,18 @@ export const rawTransaction = async (
|
|||||||
await connection.commit();
|
await connection.commit();
|
||||||
|
|
||||||
response = true;
|
response = true;
|
||||||
} catch (e) {
|
} catch (err: any) {
|
||||||
await connection.rollback().catch(() => {});
|
await connection.rollback().catch(() => {});
|
||||||
|
|
||||||
const transactionErrorMessage = (e as any).sql || transactionError(transactions, parameters);
|
const transactionErrorMessage = err.sql || transactionError(transactions, parameters);
|
||||||
|
|
||||||
console.error(
|
printError(invokingResource, cb, isPromise, transactionErrorMessage, err.message);
|
||||||
`${invokingResource} was unable to execute a transaction!\n${(e as Error).message}\n${transactionErrorMessage}^0`
|
|
||||||
);
|
|
||||||
|
|
||||||
TriggerEvent('oxmysql:transaction-error', {
|
TriggerEvent('oxmysql:transaction-error', {
|
||||||
query: transactionErrorMessage,
|
query: transactionErrorMessage,
|
||||||
parameters: parameters,
|
parameters: parameters,
|
||||||
message: (e as Error).message,
|
message: err.message,
|
||||||
err: e,
|
err: err,
|
||||||
resource: invokingResource,
|
resource: invokingResource,
|
||||||
});
|
});
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
+14
-1
@@ -1,8 +1,21 @@
|
|||||||
import { PoolConnection, RowDataPacket } from 'mysql2/promise';
|
import { PoolConnection, RowDataPacket } from 'mysql2/promise';
|
||||||
import { mysql_debug, mysql_slow_query_warning, mysql_ui } from '../config';
|
import { mysql_debug, mysql_slow_query_warning, mysql_ui } from '../config';
|
||||||
import type { CFXParameters } from '../types';
|
import type { CFXCallback, CFXParameters } from '../types';
|
||||||
import { dbVersion } from '../database';
|
import { dbVersion } from '../database';
|
||||||
|
|
||||||
|
export function printError(
|
||||||
|
invokingResource: string,
|
||||||
|
cb: CFXCallback | undefined,
|
||||||
|
isPromise: boolean | undefined,
|
||||||
|
...args: string[]
|
||||||
|
) {
|
||||||
|
const err = `${invokingResource} was unable to execute a query!\nQuery: ${args.join('\n')}`;
|
||||||
|
|
||||||
|
if (cb && isPromise) return cb(null, err);
|
||||||
|
|
||||||
|
console.error(err);
|
||||||
|
}
|
||||||
|
|
||||||
export const profilerStatements = [
|
export const profilerStatements = [
|
||||||
'SET profiling_history_size = 0',
|
'SET profiling_history_size = 0',
|
||||||
'SET profiling = 0',
|
'SET profiling = 0',
|
||||||
|
|||||||
Reference in new issue
Block a user