[英]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.