mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-01 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 { profileBatchStatements, runProfiler } from '../logger';
|
||||
import { printError, profileBatchStatements, runProfiler } from '../logger';
|
||||
import { CFXCallback, CFXParameters } from '../types';
|
||||
import { parseResponse } from '../utils/parseResponse';
|
||||
import { executeType, parseExecute } from '../utils/parseExecute';
|
||||
@@ -15,8 +15,12 @@ export const rawExecute = async (
|
||||
unpack?: boolean
|
||||
) => {
|
||||
if (typeof query !== 'string')
|
||||
throw new Error(
|
||||
`${invokingResource} was unable to execute a query!\nExpected query to be a string but received ${typeof query} instead.`
|
||||
return printError(
|
||||
invokingResource,
|
||||
cb,
|
||||
isPromise,
|
||||
query,
|
||||
`Expected query to be a string but received ${typeof query} instead.`
|
||||
);
|
||||
|
||||
const type = executeType(query);
|
||||
@@ -81,18 +85,15 @@ export const rawExecute = async (
|
||||
}
|
||||
}
|
||||
} 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', {
|
||||
query: query,
|
||||
parameters: parameters,
|
||||
message: (err).message,
|
||||
err: (err),
|
||||
message: err.message,
|
||||
err: err,
|
||||
resource: invokingResource,
|
||||
});
|
||||
|
||||
if (cb && isPromise) return cb(null, error);
|
||||
console.error(error);
|
||||
} finally {
|
||||
connection.release();
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { pool, isServerConnected, waitForConnection } from '.';
|
||||
import { parseArguments } from '../utils/parseArguments';
|
||||
import { parseResponse } from '../utils/parseResponse';
|
||||
import { logQuery, runProfiler } from '../logger';
|
||||
import { logQuery, printError, runProfiler } from '../logger';
|
||||
import type { CFXCallback, CFXParameters } from '../types';
|
||||
import type { QueryType } from '../types';
|
||||
import { scheduleTick } from '../utils/scheduleTick';
|
||||
@@ -16,8 +16,12 @@ export const rawQuery = async (
|
||||
isPromise?: boolean
|
||||
) => {
|
||||
if (typeof query !== 'string')
|
||||
throw new Error(
|
||||
`${invokingResource} was unable to execute a query!\nExpected query to be a string but received ${typeof query} instead.`
|
||||
return printError(
|
||||
invokingResource,
|
||||
cb,
|
||||
isPromise,
|
||||
query,
|
||||
`Expected query to be a string but received ${typeof query} instead.`
|
||||
);
|
||||
|
||||
[query, parameters, cb] = parseArguments(invokingResource, query, parameters, cb);
|
||||
@@ -50,9 +54,7 @@ export const rawQuery = async (
|
||||
}
|
||||
}
|
||||
} catch (err: any) {
|
||||
const error = `${invokingResource} was unable to execute a query!\n${err.message}\n${`${query} ${JSON.stringify(
|
||||
parameters
|
||||
)}`}`;
|
||||
printError(invokingResource, cb, isPromise, query, JSON.stringify(parameters), err.message);
|
||||
|
||||
TriggerEvent('oxmysql:error', {
|
||||
query: query,
|
||||
@@ -61,9 +63,6 @@ export const rawQuery = async (
|
||||
err: err,
|
||||
resource: invokingResource,
|
||||
});
|
||||
|
||||
if (cb && isPromise) return cb(null, error);
|
||||
console.error(error);
|
||||
} finally {
|
||||
connection.release();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { pool, isServerConnected, waitForConnection } from '.';
|
||||
import { profileBatchStatements, runProfiler } from '../logger';
|
||||
import { printError, profileBatchStatements, runProfiler } from '../logger';
|
||||
import { CFXCallback, CFXParameters, TransactionQuery } from '../types';
|
||||
import { parseTransaction } from '../utils/parseTransaction';
|
||||
import { scheduleTick } from '../utils/scheduleTick';
|
||||
@@ -43,20 +43,18 @@ export const rawTransaction = async (
|
||||
await connection.commit();
|
||||
|
||||
response = true;
|
||||
} catch (e) {
|
||||
} catch (err: any) {
|
||||
await connection.rollback().catch(() => {});
|
||||
|
||||
const transactionErrorMessage = (e as any).sql || transactionError(transactions, parameters);
|
||||
|
||||
console.error(
|
||||
`${invokingResource} was unable to execute a transaction!\n${(e as Error).message}\n${transactionErrorMessage}^0`
|
||||
);
|
||||
const transactionErrorMessage = err.sql || transactionError(transactions, parameters);
|
||||
|
||||
printError(invokingResource, cb, isPromise, transactionErrorMessage, err.message);
|
||||
|
||||
TriggerEvent('oxmysql:transaction-error', {
|
||||
query: transactionErrorMessage,
|
||||
parameters: parameters,
|
||||
message: (e as Error).message,
|
||||
err: e,
|
||||
message: err.message,
|
||||
err: err,
|
||||
resource: invokingResource,
|
||||
});
|
||||
} finally {
|
||||
|
||||
+14
-1
@@ -1,8 +1,21 @@
|
||||
import { PoolConnection, RowDataPacket } from 'mysql2/promise';
|
||||
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';
|
||||
|
||||
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 = [
|
||||
'SET profiling_history_size = 0',
|
||||
'SET profiling = 0',
|
||||
|
||||
Reference in new issue
Block a user