mirror of
https://github.com/darksaid98/oxmysql.git
synced 2026-12-02 06:28:54 +00:00
fix(db): handle errors during callbacks
Handles errors that would previously disappear into the void or show as 'undefined'. In Lua this would only occur if yielding during the query callback response. Cross runtime error handling is suffering.
This commit is contained in:
1 parent
db0f29db42
commit
d80615bddc
5 files changed
+51
-34
No files matched your search
+1
-1
@@ -3,7 +3,7 @@ game 'common'
|
||||
|
||||
name 'oxmysql'
|
||||
description 'Database wrapper for FiveM utilising node-mysql2 offering improved performance and security.'
|
||||
version '2.3.3'
|
||||
version '2.3.4'
|
||||
url 'https://github.com/overextended/oxmysql'
|
||||
author 'overextended'
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ export const rawExecute = async (
|
||||
return await new Promise((resolve, reject) => {
|
||||
pool.getConnection((err, connection) => {
|
||||
if (err) return reject(err.message);
|
||||
if (parameters.length === 0) return reject(`Query received no parameters.`);
|
||||
|
||||
const placeholders = query.split('?').length - 1;
|
||||
|
||||
@@ -68,11 +69,16 @@ export const rawExecute = async (
|
||||
});
|
||||
});
|
||||
})
|
||||
.then((response) => {
|
||||
.then(async (response) => {
|
||||
if (cb)
|
||||
try {
|
||||
cb(response as QueryResponse);
|
||||
} catch (err) {}
|
||||
await cb(response);
|
||||
} catch (err) {
|
||||
if (typeof err === 'string') {
|
||||
if (err.includes('SCRIPT ERROR:')) return console.log(err);
|
||||
console.log(`^1SCRIPT ERROR in invoking resource ${invokingResource}: ${err}^0`);
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
const error = `${invokingResource} was unable to execute a query!\n${err}\n${`${query}`}`;
|
||||
@@ -86,6 +92,6 @@ export const rawExecute = async (
|
||||
});
|
||||
|
||||
if (cb && throwError) return cb(null, error);
|
||||
throw new Error(error);
|
||||
throw new Error(error);
|
||||
});
|
||||
};
|
||||
+27
-19
@@ -23,26 +23,34 @@ export const rawQuery = async (
|
||||
if (err) return reject(err);
|
||||
|
||||
logQuery(invokingResource, query, executionTime, parameters);
|
||||
try {
|
||||
resolve(cb ? cb(parseResponse(type, result)) : null);
|
||||
} catch (err) {}
|
||||
resolve(cb ? parseResponse(type, result) : null);
|
||||
});
|
||||
}).catch((err) => {
|
||||
if (typeof err === 'string') {
|
||||
return console.log(err)
|
||||
}
|
||||
|
||||
const error = `${invokingResource} was unable to execute a query!\n${err.message}\n${`${query} ${JSON.stringify(parameters)}`}`;
|
||||
})
|
||||
.then(async (result) => {
|
||||
if (cb)
|
||||
try {
|
||||
await cb(result);
|
||||
} catch (err) {
|
||||
if (typeof err === 'string') {
|
||||
if (err.includes('SCRIPT ERROR:')) return console.log(err);
|
||||
console.log(`^1SCRIPT ERROR in invoking resource ${invokingResource}: ${err}^0`);
|
||||
}
|
||||
}
|
||||
})
|
||||
.catch((err) => {
|
||||
const error = `${invokingResource} was unable to execute a query!\n${err.message}\n${`${query} ${JSON.stringify(
|
||||
parameters
|
||||
)}`}`;
|
||||
|
||||
TriggerEvent('oxmysql:error', {
|
||||
query: query,
|
||||
parameters: parameters,
|
||||
message: err.message,
|
||||
err: err,
|
||||
resource: invokingResource,
|
||||
TriggerEvent('oxmysql:error', {
|
||||
query: query,
|
||||
parameters: parameters,
|
||||
message: err.message,
|
||||
err: err,
|
||||
resource: invokingResource,
|
||||
});
|
||||
|
||||
if (cb && throwError) return cb(null, error);
|
||||
throw new Error(error);
|
||||
});
|
||||
|
||||
if (cb && throwError) return cb(null, error);
|
||||
throw new Error(error);
|
||||
});
|
||||
};
|
||||
@@ -25,11 +25,11 @@ export const rawTransaction = async (
|
||||
await connection.beginTransaction();
|
||||
|
||||
for (const transaction of transactions) {
|
||||
//@ts-expect-error
|
||||
const [result, fields, executionTime] = await connection.query(transaction.query, transaction.params);
|
||||
//@ts-expect-error
|
||||
logQuery(invokingResource, transaction.query, executionTime, transaction.params);
|
||||
}
|
||||
//@ts-expect-error
|
||||
const [result, fields, executionTime] = await connection.query(transaction.query, transaction.params);
|
||||
//@ts-expect-error
|
||||
logQuery(invokingResource, transaction.query, executionTime, transaction.params);
|
||||
}
|
||||
|
||||
await connection.commit();
|
||||
|
||||
@@ -39,9 +39,7 @@ export const rawTransaction = async (
|
||||
|
||||
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`
|
||||
`${invokingResource} was unable to execute a transaction!\n${(e as Error).message}\n${transactionErrorMessage}^0`
|
||||
);
|
||||
|
||||
TriggerEvent('oxmysql:transaction-error', {
|
||||
@@ -58,5 +56,10 @@ export const rawTransaction = async (
|
||||
if (cb)
|
||||
try {
|
||||
cb(response);
|
||||
} catch {}
|
||||
} catch (err) {
|
||||
if (typeof err === 'string') {
|
||||
if (err.includes('SCRIPT ERROR:')) return console.log(err);
|
||||
console.log(`^1SCRIPT ERROR in invoking resource ${invokingResource}: ${err}^0`);
|
||||
}
|
||||
}
|
||||
};
|
||||
+1
-1
@@ -15,4 +15,4 @@ export type TransactionQuery = {
|
||||
// working with this type is impossible but at least we can pretend to be strictly typed
|
||||
export type CFXParameters = any[];
|
||||
|
||||
export type CFXCallback = (result: QueryResponse | number | null, err?: string) => void;
|
||||
export type CFXCallback = (result: unknown, err?: string) => Promise<unknown>;
|
||||
Reference in new issue
Block a user