Error handling for fetch() in Aurelia

前端 未结 1 1597
孤城傲影
孤城傲影 2021-02-01 22:43

I have an API that includes a useful description of what went wrong when an error is raised by the server (status = 500). The description comes as part of the response text. My

相关标签:
1条回答
  • 2021-02-01 22:58

    This should do the trick:

    function callRemoteService(apiName, timeout = 5000) {
      return Promise.race([
        this.http.fetch(apiName)
          .then(
            r => r.json(),
            r => r.text().then(text => throw new Error(text))
          ),
        this.waitForServer(timeout)
      ]);
    }
    

    by the way, I like what you're doing with Promise.race- nice technique!

    0 讨论(0)
提交回复
热议问题