|
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)
};
react-dynamic-data-workshop/solution/src/utils/data_helpers.js
Lines 4 to 18 in fd8edc8
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: