@@ -52,6 +52,22 @@ var Duplex = stream.Duplex;
52
52
var util = require ( 'util' ) ;
53
53
var version = require ( '../package.json' ) . version ;
54
54
55
+ /**
56
+ * Create an Error object from a status object
57
+ * @private
58
+ * @param {grpc~StatusObject } status The status object
59
+ * @return {Error } The resulting Error
60
+ */
61
+ function createStatusError ( status ) {
62
+ let statusName = _ . invert ( constants . status ) [ status . code ] ;
63
+ let message = `${ status . code } ${ statusName } : ${ status . details } ` ;
64
+ let error = new Error ( message ) ;
65
+ error . code = status . code ;
66
+ error . metadata = status . metadata ;
67
+ error . details = status . details ;
68
+ return error ;
69
+ }
70
+
55
71
/**
56
72
* Initial response metadata sent by the server when it starts processing the
57
73
* call
@@ -251,9 +267,7 @@ function _emitStatusIfDone() {
251
267
if ( status . code === constants . status . OK ) {
252
268
this . push ( null ) ;
253
269
} else {
254
- var error = new Error ( status . details ) ;
255
- error . code = status . code ;
256
- error . metadata = status . metadata ;
270
+ var error = createStatusError ( status ) ;
257
271
this . emit ( 'error' , error ) ;
258
272
}
259
273
this . emit ( 'status' , status ) ;
@@ -556,9 +570,7 @@ Client.prototype.makeUnaryRequest = function(method, serialize, deserialize,
556
570
}
557
571
}
558
572
if ( status . code !== constants . status . OK ) {
559
- error = new Error ( status . details ) ;
560
- error . code = status . code ;
561
- error . metadata = status . metadata ;
573
+ error = new createStatusError ( status ) ;
562
574
callback ( error ) ;
563
575
} else {
564
576
callback ( null , deserialized ) ;
@@ -645,9 +657,7 @@ Client.prototype.makeClientStreamRequest = function(method, serialize,
645
657
}
646
658
}
647
659
if ( status . code !== constants . status . OK ) {
648
- error = new Error ( response . status . details ) ;
649
- error . code = status . code ;
650
- error . metadata = status . metadata ;
660
+ error = createStatusError ( status ) ;
651
661
callback ( error ) ;
652
662
} else {
653
663
callback ( null , deserialized ) ;
0 commit comments