How to handle errors using rxjs and promises?

末鹿安然 提交于 2019-12-24 20:12:30

问题


I am making a http request to a backend api. I have deliberately changed the url to one that returns a 503 so that i can properly handle errors. However the 503 error response is not being returned here.

const requestOptions = {
  url: "https://httpstat.us//503",
  method: "get",
  // raxConfig: retryConfig,
};

const $metrics = defer(() => from(axios(requestOptions))));

const metrics = await $metrics.pipe(
  map((val: any) => {
    if (val.data.metrics.length === 0) {
      throw val;
    }
    return val;
  }),
  retryWhen((errors) => errors.pipe(delay(2000), take(10))),
).toPromise();

console.log("metrics", metrics)

The console.log at the end returns

undefined

I expect to see the response of the http promise which is a 503 status error.


Context: i am using rxjs to handle when to retry the http request. The data might not be ready (length === 0) i retry up to 10 times until data is there. The logic is more complicated inside the map function but i have omitted some of it as its not relevant.

来源:https://stackoverflow.com/questions/56341838/how-to-handle-errors-using-rxjs-and-promises

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!