简体   繁体   中英

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. Pls let me know in HttpInterceptor what are the types of error we can handle and in ErrorHandler what errors we can handle. 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); } } }

There is a similar question here that maybe is what you're looking for! :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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