简体   繁体   English

处理异步/等待 Angular HttpClient 方法中的错误

[英]Handling errors in an async/await Angular HttpClient method

I am attempting to use an async/await pattern in order to handle a scenario that might be considered "callback hell" if implemented otherwise.我正在尝试使用 async/await 模式来处理如果以其他方式实现可能被视为“回调地狱”的场景。

Here is an extremely dumbed down version of the code.这是代码的一个极其简化的版本。 The real code has about 5 conditional HttpClient calls based on the data from the first call (not my api...) which is the reason why I am using the async/await pattern in the first place.基于第一次调用(不是我的 api...)的数据,真实代码有大约 5 个条件 HttpClient 调用,这就是我首先使用 async/await 模式的原因。

async blah(): Promise<boolean> {
    try {
        let resp = await this.http.get("https://httpstat.us/500").toPromise();
        console.warn("you should not see this");

        // the real code will logically call the api multiple times based on conditonal data from resp
        // hence the attempted usage of async/await to avoid "callback hell"
        // blah() will eventually return an object.

        return true;
    }
    catch (err) {
        console.error("caught inside blah()");
        throw err;
    }
}

ionViewDidLoad() {
    this.blah().then(data => {
        console.warn('okokokok');
    }).catch(error => {
        console.error(error)
    });
}

What happens, I can see the call actually 500, but the code continues and the following is printed to the console:会发生什么,我可以看到调用实际上是 500,但代码继续运行,以下内容打印到控制台:

polyfills.js:3 GET https://httpstat.us/500/ 500 (Internal Server Error)
main.js:927 you should not see this
main.js:940 okokokok

As you can see, it isn't catching the 500 (or any other http status I have tested with)如您所见,它没有捕获 500(或我测试过的任何其他 http 状态)

The device I am testing with is a Pixel 2 running P and the console data is coming from a Chrome device inspector session.我正在测试的设备是运行 P 的 Pixel 2,控制台数据来自 Chrome 设备检查器会话。

Any advice would be greatly appreciated.任何建议将不胜感激。

** Edit: This is clearly an issue with the combination of ionic and angular... It should work... ** 编辑:这显然是离子和角度组合的问题......它应该可以工作......

** Edit: it turns out to 100% be an Angular issue... Not the framework itself but how an interceptor was implemented. ** 编辑:结果 100% 是一个 Angular 问题......不是框架本身,而是拦截器是如何实现的。 I will leave this here instead of deleting the question in the rare case someone else requires it.我将把这个留在这里,而不是在其他人需要的极少数情况下删除这个问题。

If i uderstood your question correctly, you want to do cascade calls, so you make the http request and based on the response you want to do another http call.如果我正确理解了您的问题,您想要进行级联调用,因此您发出 http 请求并根据您想要进行另一个 http 调用的响应。 If that is the case, then you should consider using switchMap operator:如果是这种情况,那么您应该考虑使用switchMap运算符:

this.http.get("https://httpstat.us/500").pipe( switchMap( result => { if(result.a === 5) { return this.http.get("some server api url"); } return return this.http.get("another server api url"); }) )

You handle the errors then in rxjs way.然后以rxjs方式处理错误。

See cascading calls查看级联调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM