简体   繁体   English

如何防止在错误处理程序中引发错误?

[英]How can I prevent throwing errors in errorhandler?

Currently, in one of my service methods, I have something like this: 当前,在我的一种服务方法中,我有如下内容:

this.get(foo).catch((e) => this.errorHandler(e));

Where errorHandler is: 其中errorHandler是:

errorHandler(error: HttpErrorResponse) {

    if (error.status === 422) {
      this.openSnackBar('Number already exists');
      return Observable.throw(error.message || 'Server Error');
    } else {
      return Observable.throw(error.message || 'Server Error');
    }
  }

While this works just fine, it throws the error and I can see it reported in my console window. 虽然这很好用,但会引发错误,并且可以在控制台窗口中看到报告。 I was wondering for situations where I am handling the error in a user friendly message like the snackbar example above, how can I prevent it from being thrown/shown in the console window? 我想知道在处理用户友好消息中的错误(例如上面的小吃店示例)时,如何防止在控制台窗口中引发/显示错误?

I just want to catch the error, check the status and return a snackbar with userfriendly message. 我只想捕获错误,检查状态并返回带有用户友好消息的小吃店。 I have noticed if I remove the throw above, the code stops working. 我注意到如果删除上面的代码,代码将停止工作。

After you've handled your exception (eg by showing an alert, toast or whatever), return an Observable.empty() instead of Observable.throw . 在处理完异常之后(例如,通过显示警报,吐司等),请返回Observable.empty()而不是Observable.throw Observable.empty is an observable that "completes" immediately, without "next"ing any values; Observable.empty是一个可观察的对象,可立即“完成”,而无需“下一步”添加任何值; this will stop your observable stream correctly. 这将正确停止您的可观察流。

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

相关问题 如何测试 Angular 2 ErrorHandler 实现? - How can I test an Angular 2 ErrorHandler implementation? 如何处理各种错误以防止应用崩溃? - How can I handle various errors to prevent app from crash? 如何阻止asp.net阻止访问文件然后在构建时抛出访问被拒绝的错误? - How can I stop asp.net from blocking access to files then throwing access denied errors on build? Angular2自定义ErrorHandler,为什么我可以登录控制台但不能登录模板? - Angular2 custom ErrorHandler, why can I log to console but not to template? 如何限制ErrorHandler的范围? - How to limit scope of ErrorHandler? 使用自定义ErrorHandler Angular 5无法显示后端错误的实质性小吃栏 - Can't show a material snackbar for the backend errors using custom ErrorHandler Angular 5 Angular 5:HttpInterceptor可以替换ErrorHandler吗 - Angular 5: Can HttpInterceptor Replace ErrorHandler 如何在自定义 ErrorHandler 中解决 Angular 8 应用程序中的“无法解析 […] 的所有参数”问题? - How to solve “Can't resolve all parameters for […]” issue in Angular 8 application in a custom ErrorHandler? 如何防止构建有 tslint 错误? - How to prevent build with there are tslint errors? 无法在角度2的ErrorHandler中定义带参数的构造函数 - Can not define a constructor with parameters in ErrorHandler in angular 2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM