Skip to content

Commit

Permalink
chore: add query to error if enabled in debug
Browse files Browse the repository at this point in the history
  • Loading branch information
bombillazo committed Feb 18, 2024
1 parent 698360d commit 959fa96
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 10 deletions.
8 changes: 7 additions & 1 deletion client/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,18 @@ export class PostgresError extends Error {
*/
public fields: Notice;

/**
* The query that caused the error
*/
public query: string | undefined;

/**
* Create a new PostgresError
*/
constructor(fields: Notice) {
constructor(fields: Notice, query?: string) {
super(fields.message);
this.fields = fields;
this.query = query;
this.name = "PostgresError";
}
}
Expand Down
20 changes: 18 additions & 2 deletions connection/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,15 @@ export class Connection {
while (current_message.type !== INCOMING_QUERY_MESSAGES.READY) {
switch (current_message.type) {
case ERROR_MESSAGE:
error = new PostgresError(parseNoticeMessage(current_message));
error = new PostgresError(
parseNoticeMessage(current_message),
isDebugOptionEnabled(
"queryInError",
this.#connection_params.controls?.debug,
)
? query.text
: undefined,
);
break;
case INCOMING_QUERY_MESSAGES.COMMAND_COMPLETE: {
result.handleCommandComplete(
Expand Down Expand Up @@ -881,7 +889,15 @@ export class Connection {
while (current_message.type !== INCOMING_QUERY_MESSAGES.READY) {
switch (current_message.type) {
case ERROR_MESSAGE: {
error = new PostgresError(parseNoticeMessage(current_message));
error = new PostgresError(
parseNoticeMessage(current_message),
isDebugOptionEnabled(
"queryInError",
this.#connection_params.controls?.debug,
)
? query.text
: undefined,
);
break;
}
case INCOMING_QUERY_MESSAGES.BIND_COMPLETE:
Expand Down
8 changes: 5 additions & 3 deletions debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
export type DebugControls = DebugOptions | boolean;

type DebugOptions = {
/** Log queries */
/** Log all queries */
queries?: boolean;
/** Log INFO, NOTICE, and WARNING raised database messages */
/** Log all INFO, NOTICE, and WARNING raised database messages */
notices?: boolean;
/** Log results */
/** Log all results */
results?: boolean;
/** Include the SQL query that caused an error in the PostgresError object */
queryInError?: boolean;
};

export const isDebugOptionEnabled = (
Expand Down
9 changes: 5 additions & 4 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1403,8 +1403,10 @@ enabled by using the `debug` option in the Client `controls` parameter. Pass
options:

- `queries` : Logs all SQL queries executed by the client
- `notices` : Logs database messages (INFO, NOTICE, WARNING))
- `results` : Logs the result of the queries
- `notices` : Logs all database messages (INFO, NOTICE, WARNING))
- `results` : Logs all the result of the queries
- `queryInError` : Includes the SQL query that caused an error in the
PostgresError object

### Example

Expand All @@ -1419,7 +1421,6 @@ const client = new Client({
port: 5432,
password: "postgres",
controls: {
// the same as `debug: true`
debug: {
queries: true,
notices: true,
Expand All @@ -1430,7 +1431,7 @@ const client = new Client({

await client.connect();

const result = await client.queryObject`SELECT public.get_some_user()`;
await client.queryObject`SELECT public.get_uuid()`;

await client.end();
```
Expand Down
38 changes: 38 additions & 0 deletions tests/query_client_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
import {
assert,
assertEquals,
assertInstanceOf,
assertObjectMatch,
assertRejects,
assertThrows,
Expand Down Expand Up @@ -284,6 +285,43 @@ Deno.test(
),
);

Deno.test(
"Debug query not in error",
withClient(async (client) => {
const invalid_query = "SELECT this_has $ 'syntax_error';";
try {
await client.queryObject(invalid_query);
} catch (error) {
assertInstanceOf(error, PostgresError);
assertEquals(error.message, 'syntax error at or near "$"');
assertEquals(error.query, undefined);
}
}),
);

Deno.test(
"Debug query in error",
withClient(
async (client) => {
const invalid_query = "SELECT this_has $ 'syntax_error';";
try {
await client.queryObject(invalid_query);
} catch (error) {
assertInstanceOf(error, PostgresError);
assertEquals(error.message, 'syntax error at or near "$"');
assertEquals(error.query, invalid_query);
}
},
{
controls: {
debug: {
queryInError: true,
},
},
},
),
);

Deno.test(
"Array arguments",
withClient(async (client) => {
Expand Down
1 change: 1 addition & 0 deletions tests/test_deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export * from "../deps.ts";
export {
assert,
assertEquals,
assertInstanceOf,
assertNotEquals,
assertObjectMatch,
assertRejects,
Expand Down

1 comment on commit 959fa96

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No typecheck tests failure

This error was most likely caused by incorrect type stripping from the SWC crate

Please report the following failure to https://github.com/denoland/deno with a reproduction of the current commit

Failure log

Please sign in to comment.