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

Handle network errors better #31

Open
oliverjam opened this issue Aug 5, 2020 · 1 comment
Open

Handle network errors better #31

oliverjam opened this issue Aug 5, 2020 · 1 comment

Comments

@oliverjam
Copy link
Collaborator

const checkResponse = response => {
if (response.status !== 200) {
console.log(`Error with the request! ${response.status}`);
return;
}
return response.json();
};
export const getData = url => {
return fetch(`${url}?access_token=${accessToken}`)
.then(checkResponse)
.catch(err => {
throw new Error(`fetch getUserData failed ${err}`);
});
};

The fetch util currently swallows non-200 responses silently (returning undefined) and catches then rethrows fetch errors. I think it would be a better example if we changed it to something like this:

const checkResponse = (res) => {
  if (!res.ok) throw new Error(`Network error: ${response.status}`);
  return res.json();
}

export const getData = url => { 
  return fetch(`${url}?access_token=${accessToken}`) 
    .then(checkResponse)
 }; 
@oliverjam oliverjam reopened this Sep 19, 2020
@oliverjam
Copy link
Collaborator Author

I forgot to update the solution (I only changed the readme) which meant a bunch of people ended up using the old version in their projects. Reminder to myself to update the solution too at some point

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

No branches or pull requests

1 participant