Since response.json() returns Promise, you can handle the error with catch and return a dummy data object.
fetch('url').then(response => { return response.json().catch(err => { console.error(`'${err}' happened, but no big deal!`); return {}; }); }).then(data => { console.log(data); });
Update
As mentioned below, if you try to read response twice, you get an error message: TypeError: Already Read .
As a workaround, you can clone original answer and call json on the cloned object.
fetch('url').then(response => { const responseCopy = response.clone(); return responseCopy.json().catch(_ => response.text()); }).then(data => { console.log(data); });
Roman Paradeev
source share