Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(AjaxObservable): notify with error if fails to parse json response #3139

Merged
merged 2 commits into from
Jul 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 60 additions & 3 deletions spec/observables/dom/ajax-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,34 @@ describe('ajax', () => {
expect(complete).to.be.true;
});

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

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: any;
const obj = {
Expand Down Expand Up @@ -312,6 +340,36 @@ describe('ajax', () => {
expect(complete).to.be.true;
});

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

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 @@ -1104,8 +1162,6 @@ class MockXMLHttpRequest {
protected defaultResponseValue() {
if (this.async === false) {
this.response = this.responseText;
} else {
throw new Error('unhandled type "' + this.responseType + '"');
}
}

Expand All @@ -1121,8 +1177,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
48 changes: 35 additions & 13 deletions src/internal/observable/dom/AjaxObservable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,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 @@ -320,7 +323,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 @@ -346,7 +354,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 @@ -388,7 +401,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 @@ -474,17 +492,21 @@ export class AjaxError extends Error {
}
}

function parseJson(xhr: XMLHttpRequest) {
// HACK(benlesh): TypeScript shennanigans
// tslint:disable-next-line:no-any XMLHttpRequest is defined to always have 'response' inferring xhr as never for the else clause.
if ('response' in (xhr as any)) {
//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 as any).responseText || 'null');
}
}

function parseXhrResponse(responseType: string, xhr: XMLHttpRequest) {
switch (responseType) {
case 'json':
// HACK(benlesh): TypeScript shennanigans
// tslint:disable-next-line:no-any XMLHttpRequest is defined to always have 'response' inferring xhr as never for the else clause.
if ('response' in (xhr as any)) {
//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 as any).responseText || 'null');
}
return tryCatch(parseJson)(xhr);
case 'xml':
return xhr.responseXML;
case 'text':
Expand Down