问题
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