Skip to content

Commit

Permalink
fix(AjaxObservable): notify with error if fails to parse json response
Browse files Browse the repository at this point in the history
Catch error when parsing json response and notify observer with thrown error.
JSON could be invalid if proxy overtakes the response and in IE responseType is empty.

closes ReactiveX#3138
  • Loading branch information
myklt committed Nov 28, 2017
1 parent 3390926 commit 31d796b
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 14 deletions.
63 changes: 60 additions & 3 deletions spec/observables/dom/ajax-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,34 @@ describe('Observable.ajax', () => {
expect(complete).to.be.true;
});

it('should fail if fails to parse response', () => {
let error;
const obj = {
url: '/flibbertyJibbet',
responseType: 'json',
method: ''
};

Rx.Observable.ajax(obj)
.subscribe((x: any) => {
throw 'should not next';
}, (err: any) => {
error = err;
}, () => {
throw 'should not complete';
});

MockXMLHttpRequest.mostRecent.respondWith({
'status': 207,
'contentType': '',
'responseType': '',
'responseText': 'Wee! I am text, but should be valid JSON!'
});

expect(error instanceof SyntaxError).to.be.true;
expect(error.message).to.equal('Unexpected token W in JSON at position 0');
});

it('should fail on 404', () => {
let error;
const obj = {
Expand Down Expand Up @@ -294,6 +322,36 @@ describe('Observable.ajax', () => {
expect(error.status).to.equal(300);
});

it('should fail if fails to parse error response', () => {
let error;
const obj = {
url: '/flibbertyJibbet',
normalizeError: (e: any, xhr: any, type: any) => {
return xhr.response || xhr.responseText;
},
responseType: 'json',
method: ''
};

Rx.Observable.ajax(obj).subscribe(x => {
throw 'should not next';
}, (err: any) => {
error = err;
}, () => {
throw 'should not complete';
});

MockXMLHttpRequest.mostRecent.respondWith({
'status': 404,
'contentType': '',
'responseType': '',
'responseText': 'Wee! I am text, but should be valid JSON!'
});

expect(error instanceof SyntaxError).to.be.true;
expect(error.message).to.equal('Unexpected token W in JSON at position 0');
});

it('should succeed no settings', () => {
const expected = JSON.stringify({ foo: 'bar' });

Expand Down Expand Up @@ -1037,8 +1095,6 @@ class MockXMLHttpRequest {
protected defaultResponseValue() {
if (this.async === false) {
this.response = this.responseText;
} else {
throw new Error('unhandled type "' + this.responseType + '"');
}
}

Expand All @@ -1054,8 +1110,9 @@ class MockXMLHttpRequest {
};
this.status = response.status || 200;
this.responseText = response.responseText;
const responseType = response.responseType !== undefined ? response.responseType : this.responseType;
if (!('response' in response)) {
switch (this.responseType) {
switch (responseType) {
case 'json':
this.jsonResponseValue(response);
break;
Expand Down
44 changes: 33 additions & 11 deletions src/observable/dom/AjaxObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,11 @@ export class AjaxSubscriber<T> extends Subscriber<Event> {
this.done = true;
const { xhr, request, destination } = this;
const response = new AjaxResponse(e, xhr, request);

destination.next(response);
if (response.response === errorObject) {
destination.error(errorObject.e);
} else {
destination.next(response);
}
}

private send(): XMLHttpRequest {
Expand Down Expand Up @@ -316,7 +319,12 @@ export class AjaxSubscriber<T> extends Subscriber<Event> {
if (progressSubscriber) {
progressSubscriber.error(e);
}
subscriber.error(new AjaxTimeoutError(this, request)); //TODO: Make betterer.
const ajaxTimeoutError = new AjaxTimeoutError(this, request); //TODO: Make betterer.
if (ajaxTimeoutError.response === errorObject) {
subscriber.error(errorObject.e);
} else {
subscriber.error(ajaxTimeoutError);
}
};
xhr.ontimeout = xhrTimeout;
(<any>xhrTimeout).request = request;
Expand All @@ -342,7 +350,12 @@ export class AjaxSubscriber<T> extends Subscriber<Event> {
if (progressSubscriber) {
progressSubscriber.error(e);
}
subscriber.error(new AjaxError('ajax error', this, request));
const ajaxError = new AjaxError('ajax error', this, request);
if (ajaxError.response === errorObject) {
subscriber.error(errorObject.e);
} else {
subscriber.error(ajaxError);
}
};
xhr.onerror = xhrError;
(<any>xhrError).request = request;
Expand Down Expand Up @@ -375,7 +388,12 @@ export class AjaxSubscriber<T> extends Subscriber<Event> {
if (progressSubscriber) {
progressSubscriber.error(e);
}
subscriber.error(new AjaxError('ajax error ' + status, this, request));
const ajaxError = new AjaxError('ajax error ' + status, this, request);
if (ajaxError.response === errorObject) {
subscriber.error(errorObject.e);
} else {
subscriber.error(ajaxError);
}
}
}
};
Expand Down Expand Up @@ -458,15 +476,19 @@ export class AjaxError extends Error {
}
}

function parseJson(xhr: XMLHttpRequest) {
if ('response' in xhr) {
//IE does not support json as responseType, parse it internally
return xhr.responseType ? xhr.response : JSON.parse(xhr.response || xhr.responseText || 'null');
} else {
return JSON.parse(xhr.responseText || 'null');
}
}

function parseXhrResponse(responseType: string, xhr: XMLHttpRequest) {
switch (responseType) {
case 'json':
if ('response' in xhr) {
//IE does not support json as responseType, parse it internally
return xhr.responseType ? xhr.response : JSON.parse(xhr.response || xhr.responseText || 'null');
} else {
return JSON.parse(xhr.responseText || 'null');
}
return tryCatch(parseJson)(xhr);
case 'xml':
return xhr.responseXML;
case 'text':
Expand Down

0 comments on commit 31d796b

Please sign in to comment.