Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix(connection/raw query) handle promise when db connection lost #258

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions src/database/rawExecute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { logError, logQuery } from '../logger';
import { CFXCallback, CFXParameters, QueryType } from '../types';
import { parseResponse } from '../utils/parseResponse';
import { executeType, parseExecute } from '../utils/parseExecute';
import { getConnection } from './connection';
import { getConnection, MySql } from './connection';
import { setCallback } from '../utils/setCallback';
import { performance } from 'perf_hooks';
import validateResultSet from 'utils/validateResultSet';
Expand Down Expand Up @@ -31,9 +31,13 @@ export const rawExecute = async (
return logError(invokingResource, cb, isPromise, err, query, parameters);
}

using connection = await getConnection(connectionId);

if (!connection) return;
let connection: MySql;
try {
connection = await getConnection(connectionId);
} catch (err: any) {
if (!cb) return;
return logError(invokingResource, cb, isPromise, err, query, parameters, true);
}

try {
const hasProfiler = await runProfiler(connection, invokingResource);
Expand Down
12 changes: 8 additions & 4 deletions src/database/rawQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { parseResponse } from '../utils/parseResponse';
import { logQuery, logError } from '../logger';
import type { CFXCallback, CFXParameters } from '../types';
import type { QueryType } from '../types';
import { getConnection } from './connection';
import { getConnection, MySql } from './connection';
import { RowDataPacket } from 'mysql2';
import { performance } from 'perf_hooks';
import validateResultSet from 'utils/validateResultSet';
Expand All @@ -26,9 +26,13 @@ export const rawQuery = async (
return logError(invokingResource, cb, isPromise, err, query, parameters);
}

using connection = await getConnection(connectionId);

if (!connection) return;
let connection: MySql;
try {
connection = await getConnection(connectionId);
} catch (err: any) {
if (!cb) return;
return logError(invokingResource, cb, isPromise, err, query, parameters, true);
}

try {
const hasProfiler = await runProfiler(connection, invokingResource);
Expand Down
10 changes: 8 additions & 2 deletions src/database/rawTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getConnection } from './connection';
import { getConnection, MySql } from './connection';
import { logError, logger, logQuery } from '../logger';
import { CFXCallback, CFXParameters, TransactionQuery } from '../types';
import { parseTransaction } from '../utils/parseTransaction';
Expand Down Expand Up @@ -28,7 +28,13 @@ export const rawTransaction = async (
return logError(invokingResource, cb, isPromise, err);
}

using connection = await getConnection();
let connection: MySql;
try {
connection = await getConnection();
} catch (err: any) {
if (!cb) return;
return logError(invokingResource, cb, isPromise, err, queries.toString(), parameters, true);
}

if (!connection) return;

Expand Down
3 changes: 2 additions & 1 deletion src/logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,9 @@ export function logError(
metadata: err,
});

if (cb && isPromise) {
if (cb) {
try {
if(isPromise) return cb(null);
return cb(null, output);
} catch (e) {}

Expand Down