@@ -60,6 +60,7 @@ export function xhr(options: XHROptions): Promise<XHRResponse> {
60
60
let res = result . res ;
61
61
let readable : NodeJS . ReadableStream = res ;
62
62
let encoding = res . headers && res . headers [ 'content-encoding' ] ;
63
+ let isCompleted = false ;
63
64
if ( encoding === 'gzip' ) {
64
65
let gunzip = zlib . createGunzip ( ) ;
65
66
res . pipe ( gunzip ) ;
@@ -72,6 +73,10 @@ export function xhr(options: XHROptions): Promise<XHRResponse> {
72
73
let data : any = [ ] ;
73
74
readable . on ( 'data' , c => data . push ( c ) ) ;
74
75
readable . on ( 'end' , ( ) => {
76
+ if ( isCompleted ) {
77
+ return ;
78
+ }
79
+ isCompleted = true ;
75
80
if ( options . followRedirects > 0 && ( res . statusCode >= 300 && res . statusCode <= 303 || res . statusCode === 307 ) ) {
76
81
let location = res . headers [ 'location' ] ;
77
82
if ( location ) {
@@ -95,13 +100,21 @@ export function xhr(options: XHROptions): Promise<XHRResponse> {
95
100
e ( response ) ;
96
101
}
97
102
} ) ;
103
+ readable . on ( 'error' , ( err ) => {
104
+ let response : XHRResponse = {
105
+ responseText : localize ( 'error' , 'Unable to access {0}. Error: {1}' , options . url , err . message ) ,
106
+ status : 500
107
+ } ;
108
+ isCompleted = true ;
109
+ e ( response ) ;
110
+ } ) ;
98
111
} ) , err => {
99
112
let message : string ;
100
113
101
114
if ( agent ) {
102
- message = localize ( 'error.cannot.connect.proxy' , 'Unable to to connect to {0} through a proxy . Error: {1}' , options . url , err . message ) ;
115
+ message = localize ( 'error.cannot.connect.proxy' , 'Unable to connect to {0} through a proxy . Error: {1}' , options . url , err . message ) ;
103
116
} else {
104
- message = localize ( 'error.cannot.connect' , 'Unable to to connect to {0}. Error: {1}' , options . url , err . message ) ;
117
+ message = localize ( 'error.cannot.connect' , 'Unable to connect to {0}. Error: {1}' , options . url , err . message ) ;
105
118
}
106
119
107
120
return Promise . reject < XHRResponse > ( {
0 commit comments