Skip to content

Commit

Permalink
add error message for mysql and postgre
Browse files Browse the repository at this point in the history
  • Loading branch information
invisal committed Oct 14, 2024
1 parent 763d4d0 commit 042989c
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 26 deletions.
56 changes: 41 additions & 15 deletions src/connections/mysql.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type Connection, type RowDataPacket } from 'mysql2';
import { QueryError, type Connection, type RowDataPacket } from 'mysql2';
import { Connection as BaseConnection, QueryResult } from '.';
import { constructRawQuery, Query } from '../query';
import { QueryType } from '../query-params';
Expand Down Expand Up @@ -189,20 +189,46 @@ export class MySQLConnection implements BaseConnection {
async query<T = Record<string, unknown>>(
query: Query
): Promise<QueryResult<T>> {
const rows = await new Promise<RowDataPacket[]>((r) =>
this.conn.query(query.query, query.parameters, (_, result) => {
if (Array.isArray(result)) {
r((result as RowDataPacket[]) ?? []);
}
return r([]);
})
);

return {
data: rows.map((r) => ({ ...r }) as T),
error: null,
query: constructRawQuery(query),
};
try {
const { rows, error } = await new Promise<{
rows: RowDataPacket[];
error: QueryError | null;
}>((r) =>
this.conn.query(
query.query,
query.parameters,
(error, result) => {
if (Array.isArray(result)) {
r({
rows: (result as RowDataPacket[]) ?? [],
error,
});
}
return r({ rows: [], error });
}
)
);

if (error) {
return {
data: [],
error: { message: error.message, name: error.name },
query: constructRawQuery(query),
};
} else {
return {
data: rows.map((r) => ({ ...r }) as T),
error: null,
query: constructRawQuery(query),
};
}
} catch {
return {
error: { message: 'Unknown Error', name: 'Error' },
data: [],
query: '',
};
}
}

async fetchDatabaseSchema(): Promise<Database> {
Expand Down
37 changes: 26 additions & 11 deletions src/connections/postgresql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,33 @@ export class PostgreSQLConnection implements Connection {
async query<T = Record<string, unknown>>(
query: Query
): Promise<QueryResult<T>> {
const { rows } = await this.client.query(
query.parameters?.length === 0
? query.query
: replacePlaceholders(query.query),
query.parameters as unknown[]
);
try {
const { rows } = await this.client.query(
query.parameters?.length === 0
? query.query
: replacePlaceholders(query.query),
query.parameters as unknown[]
);

return {
data: rows,
error: null,
query: query.query,
};
return {
data: rows,
error: null,
query: query.query,
};
} catch (e) {
if (e instanceof Error) {
return {
data: [],
error: { message: e.message, name: e.name },
query: query.query,
};
}
return {
data: [],
error: { message: 'Unknown Error', name: 'Error' },
query: query.query,
};
}
}

async fetchDatabaseSchema(): Promise<Database> {
Expand Down

0 comments on commit 042989c

Please sign in to comment.