-
Notifications
You must be signed in to change notification settings - Fork 270
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: fetch: handle invalid server responses, set responseType #329
Conversation
027d4bb
to
4040a25
Compare
@@ -33,10 +33,15 @@ function fetchRequest(method, url, args) { | |||
var error = !response.ok; | |||
var status = response.status; | |||
var respHandler = function(resp) { | |||
var isJSON = typeof resp === 'object'; |
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 feels weird. It only makes sense when I scroll down and notice this is all defining respHandler
, which is passed either the raw text, an object from JSON, or an error object.
isJSON is actually the OPPOSITE - it's not JSON, it's an object. When isJSON
is true, this calls JSON.stringify() (converting the object to JSON).
I suggest:
- Defining
respHandler
outside offetchRequest
- it distracts - Changing
isJSON
to something else (isObj
, or perhapsnotYetJSON
)
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 feedback! I have refactored for better readability. Please take another look
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.
Looks good to me with a small nit
// JSON parse can fail if response is not a valid object | ||
.catch(e => { | ||
// eslint-disable-next-line no-console | ||
console.error(e); |
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.
nit: we should avoid using console.error
as much as possible. console is not 100% reliable and can break items based on chrome and other browser extensions. example is when firebug
removes native console commands and replaces them with their own, which caused issues in the past.
9814fde
to
4425489
Compare
4425489
to
08bbcaf
Compare
responseText
as a string. SIW had unit tests which revealed this quirk/bug in this library. If response type is JSON, the object will also be returned asresponseJSON
andresponseType
will be set to JSON.