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

Add safeStreamJSON function #10

Closed
wants to merge 6 commits into from

Conversation

BrendanDavies
Copy link
Collaborator

#8 Add safeStremJSON Function

What:
Add helper function to stream/parse json responses.

Why:
This allows us to comfortably stream and parse a response, without fear of loosing valuable response information, if the stream is already ready OR not valid JSON

@BrendanDavies BrendanDavies force-pushed the streamIssue branch 2 times, most recently from 46a552b to 982f2c0 Compare December 14, 2018 20:34
@BrendanDavies
Copy link
Collaborator Author

I still need to write tests for this, but wanted to float the solution by people first.

Copy link
Collaborator

@ctreatma ctreatma left a comment

Choose a reason for hiding this comment

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

This is nice. I had a couple comments on error handling for the homepage, but I haven't used/read this code in a long while, so take them with a hefty dose of salt.

src/hyperGard.js Outdated
} : Promise.reject();
})
.catch(function() {
return Promise.reject({
Copy link
Collaborator

Choose a reason for hiding this comment

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

It might be helpful to include the underlying error here for easier debugging when homepage loads fail

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good call, will add that.

I actually don't see a lot of value in having separate onError/onSuccess handlers for the homepage.

I might just consolidate, so we get more consistent results

src/hyperGard.js Outdated
return isObject(data) ? {
data: new Resource(endpoint, data),
xhr: response
} : Promise.reject();
Copy link
Collaborator

Choose a reason for hiding this comment

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

I may be missing something, but it looks like we could add some context to this rejection

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yea absolutely, I was just addressing the streaming issue in this patch.

I can adjust that as well

@BrendanDavies
Copy link
Collaborator Author

Addressed comments, but I still need tests for safeStreamJSON library

src/hyperGard.js Outdated
var o = deepExtend({
method: 'GET',
action: 'homepage',
action: action,

Choose a reason for hiding this comment

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

Elsewhere in this file, the variable name is used to represent an action name and the variable action is used to represent an action object. Mind giving it a bit of a rename? It would be helpful to people who may be diving into this for the first time.

*/
export function safeStreamJSON(response) {
if (response && response instanceof Response && !response.bodyUsed) {
return response.text()

Choose a reason for hiding this comment

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

Curious why you went with response.text & JSON.parse instead of response.json?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Response.json calls text, and the json.parse, but if the streamed text is NOT valid json, you lose that text.

var response = new Response('Thing'); response.json()

Will result in an error Uncaught (in promise) SyntaxError: Unexpected token T in JSON at position 0 at <anonymous>:2:10

But since the body has already been used, and then text was not returned in the error message, we don't have any more context to that failure.

Choose a reason for hiding this comment

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

Huh, interesting. Thanks

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.

None yet

3 participants