@@ -15,7 +15,6 @@ import { EventEmitter } from 'node:events';
15
15
import { Defaults } from './defaults.js' ;
16
16
import * as logger from './logging.js' ;
17
17
18
- import { postgresqlErrorCodes } from './errors.js' ;
19
18
import { Queue } from './queue.js' ;
20
19
import { Query } from './query.js' ;
21
20
@@ -30,7 +29,6 @@ import {
30
29
ClientConnectionDefaults ,
31
30
ClientConnectionOptions ,
32
31
DatabaseError ,
33
- ErrorLevel ,
34
32
Message ,
35
33
Reader ,
36
34
RowDescription ,
@@ -52,11 +50,7 @@ export interface ConnectionInfo {
52
50
parameters : ReadonlyMap < string , string > ;
53
51
}
54
52
55
- export interface ClientNotice extends DatabaseError {
56
- level : ErrorLevel ;
57
- code : keyof typeof postgresqlErrorCodes ;
58
- message : string ;
59
- }
53
+ export interface ClientNotice extends DatabaseError { }
60
54
61
55
export interface DataTypeError {
62
56
dataType : DataType ;
@@ -780,11 +774,7 @@ export class ClientImpl {
780
774
}
781
775
782
776
private parseError ( buffer : Buffer ) {
783
- let level : DatabaseError [ 'level' ] | null = null ;
784
- let code : DatabaseError [ 'code' ] | null = null ;
785
- let message : DatabaseError [ 'message' ] | null = null ;
786
- let details : string | null = null ;
787
-
777
+ const params : Partial < ConstructorParameters < typeof DatabaseError > > = [ ] ;
788
778
const length = buffer . length ;
789
779
let offset = 0 ;
790
780
@@ -793,45 +783,74 @@ export class ClientImpl {
793
783
if ( next < 0 ) break ;
794
784
795
785
const value = buffer . subarray ( offset + 1 , next ) . toString ( ) ;
796
- switch ( buffer [ offset ] ) {
786
+
787
+ // See https://www.postgresql.org/docs/current/protocol-error-fields.html
788
+ const token = buffer [ offset ] ;
789
+ switch ( token ) {
790
+ // S:
797
791
case 0x53 : {
798
- if ( level === null ) {
799
- level = value as DatabaseError [ 'level' ] ;
800
- }
792
+ params [ 0 ] = value as DatabaseError [ 'level' ] ;
801
793
break ;
802
794
}
795
+ // V:
796
+ // This is present only in messages generated by PostgreSQL
797
+ // versions 9.6 and later, taking priority over the previous
798
+ // case.
803
799
case 0x56 : {
804
- level = value as DatabaseError [ 'level' ] ;
800
+ params [ 0 ] = value as DatabaseError [ 'level' ] ;
805
801
break ;
806
802
}
803
+ // C:
807
804
case 0x43 : {
808
- code = value as DatabaseError [ 'code' ] ;
805
+ params [ 1 ] = value as DatabaseError [ 'code' ] ;
809
806
break ;
810
807
}
808
+ // M:
809
+ case 0x4d : {
810
+ params [ 2 ] = value ;
811
+ break ;
812
+ }
813
+ // D:
811
814
case 0x44 : {
812
- details = value ;
815
+ params [ 3 ] = value ;
813
816
break ;
814
817
}
815
- case 0x4d : {
816
- message = value ;
818
+ // F:
819
+ case 0x46 : {
820
+ params [ 4 ] = value ;
817
821
break ;
818
822
}
819
- default :
823
+ // H:
824
+ case 0x48 : {
825
+ params [ 5 ] = value ;
820
826
break ;
827
+ }
828
+ // L:
829
+ case 0x4c : {
830
+ params [ 6 ] = parseInt ( value ) ;
831
+ break ;
832
+ }
833
+ // P:
834
+ case 0x50 : {
835
+ params [ 7 ] = value ;
836
+ break ;
837
+ }
838
+ // R:
839
+ case 0x52 : {
840
+ params [ 8 ] = parseInt ( value ) ;
841
+ break ;
842
+ }
821
843
}
822
844
823
845
offset = next + 1 ;
824
846
}
825
847
848
+ const [ level , code , message , ...optional ] = params ;
826
849
if ( level && code && message ) {
827
- return new DatabaseError (
828
- level ,
829
- code ,
830
- details ? `${ message } : ${ details } ` : message ,
831
- ) ;
850
+ return new DatabaseError ( level , code , message , ...optional ) ;
832
851
}
833
852
834
- throw new Error ( 'Unable to parse error message. ' ) ;
853
+ throw new Error ( 'Malformed error response ' ) ;
835
854
}
836
855
837
856
private handle ( buffer : Buffer , offset : number , size : number ) : number {
0 commit comments