当服务器在React js中关闭时处理错误(TypeError无法获取)

时间:2019-01-22 06:50:55

标签: reactjs error-handling crash-reports redux-saga

我需要在对话框中显示错误,指出无论服务器崩溃还是服务器关闭,服务器都已停止。 我作为控制台将请求中的错误记录如下:

export default function request(url, options) {
  return fetch(url, options)
    .then(checkStatus)
    .then(parseJSON).
    catch(error => {
      console.log("error",error)
      throw error;
    });
}

控制台为:

TypeError Failed to fetch

有人可以帮我吗?

2 个答案:

答案 0 :(得分:1)

您可以在代码中使用navigator来检查用户是否脱机或用户无法连接到您的WebService,如下所示:

//if there was a problem with server
if(navigator && navigator.onLine) {
  throw {
    code: 'SERVER_CONNECTION_PROBLEM',
    message: 'Server connection failure...'
  }
} else { // if there is a problem with internet
   throw {
     code: 'INTERNET_CONNECTION_ERROR',
     message: 'there is a problem with your INTERNET, god damn it...'
   }
}

答案 1 :(得分:0)

您可以检查错误状态,例如Firestore.instance.collection('users').where('name', isEqualTo: 'neha') .snapshots.listen( (data) => print('grower ${data.documents[0]['name']}') ); 然后做

error.status