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

fix: fetch: handle invalid server responses, set responseType #329

Merged
merged 1 commit into from
Feb 13, 2020

Conversation

aarongranick-okta
Copy link
Contributor

  • Catch error from json parsing caused by invalid/malformed server response and return a well structured error rather than the raw TypeError. SIW has unit tests for these scenarios. Adding tests here to cover the same functionality.
  • Always return 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 as responseJSON and responseType will be set to JSON.

@aarongranick-okta aarongranick-okta force-pushed the ag-siw-adjust-OKTA-276557 branch from 027d4bb to 4040a25 Compare February 7, 2020 20:18
@aarongranick-okta aarongranick-okta changed the title feat: handle invalid server responses, fix response format fix: fetch: handle invalid server responses, set responseType Feb 7, 2020
@@ -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';
Copy link
Contributor

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:

  1. Defining respHandler outside of fetchRequest - it distracts
  2. Changing isJSON to something else (isObj, or perhaps notYetJSON)

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 feedback! I have refactored for better readability. Please take another look

Copy link

@bretterer bretterer left a 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);

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.

@aarongranick-okta aarongranick-okta force-pushed the ag-siw-adjust-OKTA-276557 branch from 9814fde to 4425489 Compare February 13, 2020 22:13
@aarongranick-okta aarongranick-okta force-pushed the ag-siw-adjust-OKTA-276557 branch from 4425489 to 08bbcaf Compare February 13, 2020 22:39
@aarongranick-okta aarongranick-okta merged commit 56d77c9 into v3.0 Feb 13, 2020
@aarongranick-okta aarongranick-okta deleted the ag-siw-adjust-OKTA-276557 branch February 13, 2020 23:08
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