@@ -221,8 +221,11 @@ export class AjaxSubscriber<T> extends Subscriber<Event> {
221221 this . done = true ;
222222 const { xhr, request, destination } = this ;
223223 const response = new AjaxResponse ( e , xhr , request ) ;
224-
225- destination . next ( response ) ;
224+ if ( response . response === errorObject ) {
225+ destination . error ( errorObject . e ) ;
226+ } else {
227+ destination . next ( response ) ;
228+ }
226229 }
227230
228231 private send ( ) : XMLHttpRequest {
@@ -320,7 +323,12 @@ export class AjaxSubscriber<T> extends Subscriber<Event> {
320323 if ( progressSubscriber ) {
321324 progressSubscriber . error ( e ) ;
322325 }
323- subscriber . error ( new AjaxTimeoutError ( this , request ) ) ; //TODO: Make betterer.
326+ const ajaxTimeoutError = new AjaxTimeoutError ( this , request ) ; //TODO: Make betterer.
327+ if ( ajaxTimeoutError . response === errorObject ) {
328+ subscriber . error ( errorObject . e ) ;
329+ } else {
330+ subscriber . error ( ajaxTimeoutError ) ;
331+ }
324332 }
325333 xhr . ontimeout = xhrTimeout ;
326334 ( < any > xhrTimeout ) . request = request ;
@@ -346,7 +354,12 @@ export class AjaxSubscriber<T> extends Subscriber<Event> {
346354 if ( progressSubscriber ) {
347355 progressSubscriber . error ( e ) ;
348356 }
349- subscriber . error ( new AjaxError ( 'ajax error' , this , request ) ) ;
357+ const ajaxError = new AjaxError ( 'ajax error' , this , request ) ;
358+ if ( ajaxError . response === errorObject ) {
359+ subscriber . error ( errorObject . e ) ;
360+ } else {
361+ subscriber . error ( ajaxError ) ;
362+ }
350363 } ;
351364 xhr . onerror = xhrError ;
352365 ( < any > xhrError ) . request = request ;
@@ -388,7 +401,12 @@ export class AjaxSubscriber<T> extends Subscriber<Event> {
388401 if ( progressSubscriber ) {
389402 progressSubscriber . error ( e ) ;
390403 }
391- subscriber . error ( new AjaxError ( 'ajax error ' + status , this , request ) ) ;
404+ const ajaxError = new AjaxError ( 'ajax error ' + status , this , request ) ;
405+ if ( ajaxError . response === errorObject ) {
406+ subscriber . error ( errorObject . e ) ;
407+ } else {
408+ subscriber . error ( ajaxError ) ;
409+ }
392410 }
393411 }
394412 }
@@ -474,17 +492,21 @@ export class AjaxError extends Error {
474492 }
475493}
476494
495+ function parseJson ( xhr : XMLHttpRequest ) {
496+ // HACK(benlesh): TypeScript shennanigans
497+ // tslint:disable-next-line:no-any XMLHttpRequest is defined to always have 'response' inferring xhr as never for the else clause.
498+ if ( 'response' in ( xhr as any ) ) {
499+ //IE does not support json as responseType, parse it internally
500+ return xhr . responseType ? xhr . response : JSON . parse ( xhr . response || xhr . responseText || 'null' ) ;
501+ } else {
502+ return JSON . parse ( ( xhr as any ) . responseText || 'null' ) ;
503+ }
504+ }
505+
477506function parseXhrResponse ( responseType : string , xhr : XMLHttpRequest ) {
478507 switch ( responseType ) {
479508 case 'json' :
480- // HACK(benlesh): TypeScript shennanigans
481- // tslint:disable-next-line:no-any XMLHttpRequest is defined to always have 'response' inferring xhr as never for the else clause.
482- if ( 'response' in ( xhr as any ) ) {
483- //IE does not support json as responseType, parse it internally
484- return xhr . responseType ? xhr . response : JSON . parse ( xhr . response || xhr . responseText || 'null' ) ;
485- } else {
486- return JSON . parse ( ( xhr as any ) . responseText || 'null' ) ;
487- }
509+ return tryCatch ( parseJson ) ( xhr ) ;
488510 case 'xml' :
489511 return xhr . responseXML ;
490512 case 'text' :
0 commit comments