Skip to content

Allow xhr errors to parse json responses.#3669

Merged
mkhatib merged 4 commits intoampproject:masterfrom
mkhatib:xhr-json-errors
Jun 21, 2016
Merged

Allow xhr errors to parse json responses.#3669
mkhatib merged 4 commits intoampproject:masterfrom
mkhatib:xhr-json-errors

Conversation

@mkhatib
Copy link
Copy Markdown
Contributor

@mkhatib mkhatib commented Jun 20, 2016

A split PR from #3583

ITI: #3343


return this.fetchAmpCors_(input, init).then(response => {
return assertSuccess(response).json();
return assertSuccess(response).then(response => response.json());
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please un-nest.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

err.retriable = true;
}
if (response.headers.get('Content-Type') == 'application/json') {
return response.json().then(json => {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't return a new promise here - you are still in the constructor of the top-level promise. This is most likely not what you want. You probably just need to run promise w/o return values.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Done!

mockXhr.getResponseHeader = () => 'application/json';
const promise = assertSuccess(
createResponseInstance('{"a": 2}', mockXhr));
promise.should.be.rejected;
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dvoytenko this catches the problem with returning the resolved promise. I don't understand it well tbh, but my guess is that the promise is resolving and the .should.be.rejected won't catch this if we returned the resolved promise.

@mkhatib
Copy link
Copy Markdown
Contributor Author

mkhatib commented Jun 21, 2016

PTAL

@dvoytenko
Copy link
Copy Markdown
Contributor

LGTM

@mkhatib mkhatib merged commit a9d4c6b into ampproject:master Jun 21, 2016
@mkhatib mkhatib deleted the xhr-json-errors branch June 21, 2016 18:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants