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

Allow xhr errors to parse json responses. #3669

Merged
merged 4 commits into from
Jun 21, 2016

Conversation

mkhatib
Copy link
Contributor

@mkhatib mkhatib commented Jun 20, 2016

A split PR from #3583

ITI: #3343

@@ -157,7 +157,7 @@ export class Xhr {
setupJson_(init);

return this.fetchAmpCors_(input, init).then(response => {
return assertSuccess(response).json();
return assertSuccess(response).then(response => response.json());
Copy link
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
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
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
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
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
Contributor Author

mkhatib commented Jun 21, 2016

PTAL

@dvoytenko
Copy link
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