-
Notifications
You must be signed in to change notification settings - Fork 8
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
Conversation
46a552b
to
982f2c0
Compare
I still need to write tests for this, but wanted to float the solution by people first. |
There was a problem hiding this 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({ |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
982f2c0
to
c6cc22b
Compare
Addressed comments, but I still need tests for safeStreamJSON library |
src/hyperGard.js
Outdated
var o = deepExtend({ | ||
method: 'GET', | ||
action: 'homepage', | ||
action: action, |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh, interesting. Thanks
#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