File tree 6 files changed +74
-10
lines changed
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 {
35
35
*/
36
36
public fields : Notice ;
37
37
38
+ /**
39
+ * The query that caused the error
40
+ */
41
+ public query : string | undefined ;
42
+
38
43
/**
39
44
* Create a new PostgresError
40
45
*/
41
- constructor ( fields : Notice ) {
46
+ constructor ( fields : Notice , query ?: string ) {
42
47
super ( fields . message ) ;
43
48
this . fields = fields ;
49
+ this . query = query ;
44
50
this . name = "PostgresError" ;
45
51
}
46
52
}
Original file line number Diff line number Diff line change @@ -694,7 +694,15 @@ export class Connection {
694
694
while ( current_message . type !== INCOMING_QUERY_MESSAGES . READY ) {
695
695
switch ( current_message . type ) {
696
696
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
+ ) ;
698
706
break ;
699
707
case INCOMING_QUERY_MESSAGES . COMMAND_COMPLETE : {
700
708
result . handleCommandComplete (
@@ -881,7 +889,15 @@ export class Connection {
881
889
while ( current_message . type !== INCOMING_QUERY_MESSAGES . READY ) {
882
890
switch ( current_message . type ) {
883
891
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
+ ) ;
885
901
break ;
886
902
}
887
903
case INCOMING_QUERY_MESSAGES . BIND_COMPLETE :
Original file line number Diff line number Diff line change 8
8
export type DebugControls = DebugOptions | boolean ;
9
9
10
10
type DebugOptions = {
11
- /** Log queries */
11
+ /** Log all queries */
12
12
queries ?: boolean ;
13
- /** Log INFO, NOTICE, and WARNING raised database messages */
13
+ /** Log all INFO, NOTICE, and WARNING raised database messages */
14
14
notices ?: boolean ;
15
- /** Log results */
15
+ /** Log all results */
16
16
results ?: boolean ;
17
+ /** Include the SQL query that caused an error in the PostgresError object */
18
+ queryInError ?: boolean ;
17
19
} ;
18
20
19
21
export 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
1403
1403
options:
1404
1404
1405
1405
- ` 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
1408
1410
1409
1411
### Example
1410
1412
@@ -1419,7 +1421,6 @@ const client = new Client({
1419
1421
port: 5432 ,
1420
1422
password: " postgres" ,
1421
1423
controls: {
1422
- // the same as `debug: true`
1423
1424
debug: {
1424
1425
queries: true ,
1425
1426
notices: true ,
@@ -1430,7 +1431,7 @@ const client = new Client({
1430
1431
1431
1432
await client .connect ();
1432
1433
1433
- const result = await client .queryObject ` SELECT public.get_some_user () ` ;
1434
+ await client .queryObject ` SELECT public.get_uuid () ` ;
1434
1435
1435
1436
await client .end ();
1436
1437
```
Original file line number Diff line number Diff line change 8
8
import {
9
9
assert ,
10
10
assertEquals ,
11
+ assertInstanceOf ,
11
12
assertObjectMatch ,
12
13
assertRejects ,
13
14
assertThrows ,
@@ -284,6 +285,43 @@ Deno.test(
284
285
) ,
285
286
) ;
286
287
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
+
287
325
Deno . test (
288
326
"Array arguments" ,
289
327
withClient ( async ( client ) => {
Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ export * from "../deps.ts";
2
2
export {
3
3
assert ,
4
4
assertEquals ,
5
+ assertInstanceOf ,
5
6
assertNotEquals ,
6
7
assertObjectMatch ,
7
8
assertRejects ,
You can’t perform that action at this time.
0 commit comments