[英]Handling error in ErrorHandler and handling in HttpInterceptor
whats is the difference between the two methods of error handling in angular 7. Do we need to handle global errors in HttpInterceptor and also in angular's inbuilt ErrorHandler. angular 7 中两种错误处理方法之间的区别是什么。我们是否需要在 HttpInterceptor 和 angular 的内置 ErrorHandler 中处理全局错误。 Pls let me know in HttpInterceptor what are the types of error we can handle and in ErrorHandler what errors we can handle.
请让我知道在 HttpInterceptor 中我们可以处理哪些类型的错误,以及在 ErrorHandler 中我们可以处理哪些错误。 Do we need both or any one is enough
我们需要两者还是任何一个就足够了
export class InterceptorService implements HttpInterceptor { constructor() { } intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { return next.handle(request).pipe( retry(1), catchError((error: HttpErrorResponse) => { let errMsg = ''; if (error.error instanceof ErrorEvent) { // Client Side Error errMsg = `Client Error: ${error.error.message}`; console.log(error); } else { // Server Side Error errMsg = `Server Error: ${error.status}, Message: ${error.message}`; console.log(error); } return throwError(errMsg); }) ); } }
export class ErrorsHandler implements ErrorHandler { constructor(private injector: Injector) { } handleError(error: Error | HttpErrorResponse) { const notificationService = this.injector.get(NotificationService); if (error instanceof HttpErrorResponse) { // Server or connection error happened if (!navigator.onLine) { // Handle offline error return notificationService.error('No Internet Connection'); } else { // Handle Http Error return notificationService.error(`${error.status} - ${error.message}`); } } else { // Handle Client Error notificationService.error(error.message); } } }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.