Skip to content

Handle network errors better #31

@oliverjam

Description

@oliverjam

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)
 }; 

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions