@@ -164,7 +164,7 @@ Modem.prototype.dial = function (options, callback) {
164
164
address = address . substring ( 0 , address . length - 1 ) ;
165
165
}
166
166
}
167
-
167
+
168
168
var optionsf = {
169
169
path : address ,
170
170
method : options . method ,
@@ -340,7 +340,7 @@ Modem.prototype.buildRequest = function (options, context, data, callback) {
340
340
341
341
debug ( 'Received: %s' , result ) ;
342
342
343
- var json = utils . parseJSON ( result ) || stream . Readable . from ( buffer ) ;
343
+ var json = utils . parseJSON ( result ) || result ;
344
344
if ( finished === false ) {
345
345
finished = true ;
346
346
self . buildPayload ( null , context . isStream , context . statusCodes , false , req , res , json , callback ) ;
@@ -376,6 +376,9 @@ Modem.prototype.buildPayload = function (err, isStream, statusCodes, openStdin,
376
376
377
377
if ( statusCodes [ res . statusCode ] !== true ) {
378
378
getCause ( isStream , res , json , function ( err , cause ) {
379
+ if ( err ) {
380
+ return cb ( err , null ) ;
381
+ }
379
382
var msg = new Error (
380
383
'(HTTP code ' + res . statusCode + ') ' +
381
384
( statusCodes [ res . statusCode ] || 'unexpected' ) + ' - ' +
@@ -398,16 +401,32 @@ Modem.prototype.buildPayload = function (err, isStream, statusCodes, openStdin,
398
401
399
402
function getCause ( isStream , res , json , callback ) {
400
403
var chunks = '' ;
404
+ var done = false ;
405
+
401
406
if ( isStream ) {
402
407
res . on ( 'data' , function ( chunk ) {
403
408
chunks += chunk ;
404
409
} ) ;
410
+ res . on ( 'error' , function ( err ) {
411
+ handler ( err , null ) ;
412
+ } ) ;
405
413
res . on ( 'end' , function ( ) {
406
- callback ( null , utils . parseJSON ( chunks ) || chunks ) ;
414
+ handler ( null , utils . parseJSON ( chunks ) || chunks )
407
415
} ) ;
408
416
} else {
409
417
callback ( null , json ) ;
410
418
}
419
+
420
+ function handler ( err , data ) {
421
+ if ( done === false ) {
422
+ if ( err ) {
423
+ callback ( err ) ;
424
+ } else {
425
+ callback ( null , data ) ;
426
+ }
427
+ }
428
+ done = true ;
429
+ }
411
430
}
412
431
} ;
413
432
0 commit comments