From 959fa96ddd512db787c3a2883cf8276658bf61f7 Mon Sep 17 00:00:00 2001 From: bombillazo Date: Sun, 18 Feb 2024 00:55:46 -0400 Subject: [PATCH] chore: add query to error if enabled in debug --- client/error.ts | 8 +++++++- connection/connection.ts | 20 ++++++++++++++++++-- debug.ts | 8 +++++--- docs/README.md | 9 +++++---- tests/query_client_test.ts | 38 ++++++++++++++++++++++++++++++++++++++ tests/test_deps.ts | 1 + 6 files changed, 74 insertions(+), 10 deletions(-) diff --git a/client/error.ts b/client/error.ts index a7b97566..7fc4cccd 100644 --- a/client/error.ts +++ b/client/error.ts @@ -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"; } } diff --git a/connection/connection.ts b/connection/connection.ts index 6cc0e037..7ce3d38d 100644 --- a/connection/connection.ts +++ b/connection/connection.ts @@ -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( @@ -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: diff --git a/debug.ts b/debug.ts index b824b809..1b477888 100644 --- a/debug.ts +++ b/debug.ts @@ -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 = ( diff --git a/docs/README.md b/docs/README.md index f66b5385..477b86f4 100644 --- a/docs/README.md +++ b/docs/README.md @@ -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 @@ -1419,7 +1421,6 @@ const client = new Client({ port: 5432, password: "postgres", controls: { - // the same as `debug: true` debug: { queries: true, notices: true, @@ -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(); ``` diff --git a/tests/query_client_test.ts b/tests/query_client_test.ts index 9def424b..0e71da69 100644 --- a/tests/query_client_test.ts +++ b/tests/query_client_test.ts @@ -8,6 +8,7 @@ import { import { assert, assertEquals, + assertInstanceOf, assertObjectMatch, assertRejects, assertThrows, @@ -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) => { diff --git a/tests/test_deps.ts b/tests/test_deps.ts index 1fce7027..3ec05aaa 100644 --- a/tests/test_deps.ts +++ b/tests/test_deps.ts @@ -2,6 +2,7 @@ export * from "../deps.ts"; export { assert, assertEquals, + assertInstanceOf, assertNotEquals, assertObjectMatch, assertRejects,