The following example handles server and connectivity errors for javascript fetch requests.
function handleErrors(response) { if (!response.ok) throw new Error(response.status) return response } fetch('https://jsonplaceholder.typicode.com/todos/1') // handle network err/success .then(handleErrors) // use response of network on fetch Promise resolve .then(response => console.log("ok")) // handle fetch Promise error .catch(error => console.log('found an error: ', error)) handleErrors can be updated for handling different types of errors in different ways. This function is called whenever we get a response from the server (successful or failed).