File tree Expand file tree Collapse file tree 6 files changed +74
-10
lines changed
Expand file tree Collapse file tree 6 files changed +74
-10
lines changed Original file line number Diff line number Diff line change @@ -35,12 +35,18 @@ export class PostgresError extends Error {
3535 */
3636 public fields : Notice ;
3737
38+ /**
39+ * The query that caused the error
40+ */
41+ public query : string | undefined ;
42+
3843 /**
3944 * Create a new PostgresError
4045 */
41- constructor ( fields : Notice ) {
46+ constructor ( fields : Notice , query ?: string ) {
4247 super ( fields . message ) ;
4348 this . fields = fields ;
49+ this . query = query ;
4450 this . name = "PostgresError" ;
4551 }
4652}
Original file line number Diff line number Diff line change @@ -694,7 +694,15 @@ export class Connection {
694694 while ( current_message . type !== INCOMING_QUERY_MESSAGES . READY ) {
695695 switch ( current_message . type ) {
696696 case ERROR_MESSAGE :
697- error = new PostgresError ( parseNoticeMessage ( current_message ) ) ;
697+ error = new PostgresError (
698+ parseNoticeMessage ( current_message ) ,
699+ isDebugOptionEnabled (
700+ "queryInError" ,
701+ this . #connection_params. controls ?. debug ,
702+ )
703+ ? query . text
704+ : undefined ,
705+ ) ;
698706 break ;
699707 case INCOMING_QUERY_MESSAGES . COMMAND_COMPLETE : {
700708 result . handleCommandComplete (
@@ -881,7 +889,15 @@ export class Connection {
881889 while ( current_message . type !== INCOMING_QUERY_MESSAGES . READY ) {
882890 switch ( current_message . type ) {
883891 case ERROR_MESSAGE : {
884- error = new PostgresError ( parseNoticeMessage ( current_message ) ) ;
892+ error = new PostgresError (
893+ parseNoticeMessage ( current_message ) ,
894+ isDebugOptionEnabled (
895+ "queryInError" ,
896+ this . #connection_params. controls ?. debug ,
897+ )
898+ ? query . text
899+ : undefined ,
900+ ) ;
885901 break ;
886902 }
887903 case INCOMING_QUERY_MESSAGES . BIND_COMPLETE :
Original file line number Diff line number Diff line change 88export type DebugControls = DebugOptions | boolean ;
99
1010type DebugOptions = {
11- /** Log queries */
11+ /** Log all queries */
1212 queries ?: boolean ;
13- /** Log INFO, NOTICE, and WARNING raised database messages */
13+ /** Log all INFO, NOTICE, and WARNING raised database messages */
1414 notices ?: boolean ;
15- /** Log results */
15+ /** Log all results */
1616 results ?: boolean ;
17+ /** Include the SQL query that caused an error in the PostgresError object */
18+ queryInError ?: boolean ;
1719} ;
1820
1921export const isDebugOptionEnabled = (
Original file line number Diff line number Diff line change @@ -1403,8 +1403,10 @@ enabled by using the `debug` option in the Client `controls` parameter. Pass
14031403options:
14041404
14051405- ` queries ` : Logs all SQL queries executed by the client
1406- - ` notices ` : Logs database messages (INFO, NOTICE, WARNING))
1407- - ` results ` : Logs the result of the queries
1406+ - ` notices ` : Logs all database messages (INFO, NOTICE, WARNING))
1407+ - ` results ` : Logs all the result of the queries
1408+ - ` queryInError ` : Includes the SQL query that caused an error in the
1409+ PostgresError object
14081410
14091411### Example
14101412
@@ -1419,7 +1421,6 @@ const client = new Client({
14191421 port: 5432 ,
14201422 password: " postgres" ,
14211423 controls: {
1422- // the same as `debug: true`
14231424 debug: {
14241425 queries: true ,
14251426 notices: true ,
@@ -1430,7 +1431,7 @@ const client = new Client({
14301431
14311432await client .connect ();
14321433
1433- const result = await client .queryObject ` SELECT public.get_some_user () ` ;
1434+ await client .queryObject ` SELECT public.get_uuid () ` ;
14341435
14351436await client .end ();
14361437```
Original file line number Diff line number Diff line change 88import {
99 assert ,
1010 assertEquals ,
11+ assertInstanceOf ,
1112 assertObjectMatch ,
1213 assertRejects ,
1314 assertThrows ,
@@ -284,6 +285,43 @@ Deno.test(
284285 ) ,
285286) ;
286287
288+ Deno . test (
289+ "Debug query not in error" ,
290+ withClient ( async ( client ) => {
291+ const invalid_query = "SELECT this_has $ 'syntax_error';" ;
292+ try {
293+ await client . queryObject ( invalid_query ) ;
294+ } catch ( error ) {
295+ assertInstanceOf ( error , PostgresError ) ;
296+ assertEquals ( error . message , 'syntax error at or near "$"' ) ;
297+ assertEquals ( error . query , undefined ) ;
298+ }
299+ } ) ,
300+ ) ;
301+
302+ Deno . test (
303+ "Debug query in error" ,
304+ withClient (
305+ async ( client ) => {
306+ const invalid_query = "SELECT this_has $ 'syntax_error';" ;
307+ try {
308+ await client . queryObject ( invalid_query ) ;
309+ } catch ( error ) {
310+ assertInstanceOf ( error , PostgresError ) ;
311+ assertEquals ( error . message , 'syntax error at or near "$"' ) ;
312+ assertEquals ( error . query , invalid_query ) ;
313+ }
314+ } ,
315+ {
316+ controls : {
317+ debug : {
318+ queryInError : true ,
319+ } ,
320+ } ,
321+ } ,
322+ ) ,
323+ ) ;
324+
287325Deno . test (
288326 "Array arguments" ,
289327 withClient ( async ( client ) => {
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ export * from "../deps.ts";
22export {
33 assert ,
44 assertEquals ,
5+ assertInstanceOf ,
56 assertNotEquals ,
67 assertObjectMatch ,
78 assertRejects ,
You can’t perform that action at this time.
0 commit comments