@@ -199,10 +199,18 @@ function parse_response(code, buffer) {
199
199
break ;
200
200
case 'E' :
201
201
type = "ErrorResponse" ;
202
- args = [ { } ] ;
202
+ var err = { } ;
203
+ // args = [{}];
203
204
reader . multicstring ( ) . forEach ( function ( field ) {
204
- args [ 0 ] [ field [ 0 ] ] = field . substr ( 1 ) ;
205
+ err [ field [ 0 ] ] = field . substr ( 1 ) ;
205
206
} ) ;
207
+ // Now, convert it to a more useful object.
208
+ var obj = new Error ( ) ;
209
+ obj . code = err [ 'C' ] ;
210
+ obj . message = err [ 'M' ] ;
211
+ obj . severity = err [ 'S' ] ;
212
+
213
+ args = [ obj ] ;
206
214
break ;
207
215
case 't' :
208
216
type = "ParameterDescription" ,
@@ -286,6 +294,14 @@ function parse_response(code, buffer) {
286
294
return { type : type , args : args } ;
287
295
}
288
296
297
+ function Error ( ) {
298
+ this . severity = "" ;
299
+ this . code = "" ;
300
+ this . message = "" ;
301
+ this . toString = function ( ) {
302
+ return "[postgres.js] " + this . severity + ": " + this . message ;
303
+ }
304
+ }
289
305
290
306
function message ( ) {
291
307
var self = this ;
@@ -346,7 +362,7 @@ function message () {
346
362
message . prototype = new process . EventEmitter ;
347
363
// message.prototype.constructor = message;
348
364
349
- function Query ( sql , callback , errback ) {
365
+ function Query ( sql , callback ) {
350
366
message . call ( this ) ;
351
367
this . sql = sql ;
352
368
var q = this ;
@@ -376,7 +392,7 @@ function Query(sql, callback, errback) {
376
392
sys . debug ( "Callback: " + callback ) ;
377
393
}
378
394
379
- callback ( q . results ) ;
395
+ callback ( null , q . results ) ;
380
396
} ) ;
381
397
q . toString = function ( ) { return "Query: " + q . length } ;
382
398
q . on ( "RowDescription" , function ( desc ) {
@@ -385,6 +401,10 @@ function Query(sql, callback, errback) {
385
401
sys . debug ( "Caught RowDescription message." ) ;
386
402
}
387
403
} ) ;
404
+ q . on ( "ErrorResponse" , function ( e ) {
405
+ callback ( e ) ;
406
+ } ) ;
407
+
388
408
}
389
409
390
410
Query . prototype = new message ;
@@ -492,6 +512,15 @@ function Prepared(sql, conn /*, use_named */) {
492
512
}
493
513
} ) ;
494
514
515
+ self . on ( "ErrorResponse" , function ( err ) {
516
+ if ( currExec != null ) {
517
+ currExec . emit ( "ErrorResponse" , err ) ;
518
+ }
519
+ else {
520
+ self . error = e ;
521
+ }
522
+ } ) ;
523
+
495
524
// self.empty = function () {
496
525
// return self.__proto__.empty.call(self);
497
526
// }
@@ -594,7 +623,7 @@ function Prepared(sql, conn /*, use_named */) {
594
623
}
595
624
// Callback gets wrapped at the tx layer,
596
625
// so we know when this gets tripped.
597
- callback ( eP . results ) ;
626
+ callback ( null , eP . results ) ;
598
627
}
599
628
else if ( eP . noData === true ) {
600
629
// Callback gets wrapped at the tx layer,
@@ -615,14 +644,14 @@ function Prepared(sql, conn /*, use_named */) {
615
644
eP . on ( "newRow" , function ( row ) {
616
645
eP . results . push ( row ) ;
617
646
} ) ;
647
+ eP . on ( "ErrorResponse" , function ( e ) {
648
+ callback ( e ) ;
649
+ } ) ;
618
650
if ( exports . DEBUG > 0 ) {
619
651
sys . debug ( "Pushing execute message to eB in Prepared." ) ;
620
652
}
621
653
eB . push ( eP ) ;
622
654
}
623
- self . on ( "Error" , function ( err ) {
624
-
625
- } ) ;
626
655
}
627
656
Prepared . prototype = new message ( ) ;
628
657
Prepared . prototype . constructor = Prepared ;
@@ -1025,10 +1054,10 @@ function Connection(args) {
1025
1054
var callback = args . pop ( ) ;
1026
1055
1027
1056
if ( typeof ( callback ) === 'function' ) {
1028
- args . push ( function ( rs ) {
1057
+ args . push ( function ( err , rs ) {
1029
1058
// do the implicit sync/commit here?
1030
1059
// tx.commit();
1031
- callback ( rs , tx ) ; // So they have access to the transaction?
1060
+ callback ( err , rs , tx ) ; // So they have access to the transaction?
1032
1061
} ) ;
1033
1062
}
1034
1063
else {
@@ -1135,17 +1164,17 @@ function Connection(args) {
1135
1164
1136
1165
Easy, and very nifty.
1137
1166
*/
1138
- conn . on ( 'newListener' , function ( e , listener ) {
1139
- if ( e === 'String' ) {
1140
- // It's a string.
1141
- if ( ! ( e in [ 'newListener' ] ) ) {
1142
- conn . notify ( e , listener ) ;
1143
- }
1144
- }
1145
- } ) ;
1146
- conn . notify = function ( name ) {
1147
-
1148
- }
1167
+ // conn.on('newListener', function (e, listener) {
1168
+ // if (e === 'String') {
1169
+ // // It's a string.
1170
+ // if (!(e in ['newListener'])) {
1171
+ // conn.notify(e, listener);
1172
+ // }
1173
+ // }
1174
+ // });
1175
+ // conn.notify = function (name) {
1176
+ //
1177
+ // }
1149
1178
}
1150
1179
Connection . prototype = new process . EventEmitter ( ) ;
1151
1180
@@ -1217,11 +1246,11 @@ function Transaction (connection /*, params */) {
1217
1246
// We have an otherwise normal query.
1218
1247
// This does not require a normal Sync message
1219
1248
// or any other such magic-ery.
1220
- var q = new Query ( sql , function ( rs ) {
1249
+ var q = new Query ( sql , function ( err , rs ) {
1221
1250
if ( exports . DEBUG > 3 ) {
1222
1251
sys . debug ( "RS is:" + sys . inspect ( rs ) ) ;
1223
1252
}
1224
- callback ( rs ) ;
1253
+ callback ( err , rs ) ;
1225
1254
// callbackRun();
1226
1255
} ) ;
1227
1256
if ( exports . DEBUG > 0 ) {
0 commit comments