簡體   English   中英

ErrorHandler 中的錯誤處理和 HttpInterceptor 中的處理

[英]Handling error in ErrorHandler and handling in HttpInterceptor

angular 7 中兩種錯誤處理方法之間的區別是什么。我們是否需要在 HttpInterceptor 和 angular 的內置 ErrorHandler 中處理全局錯誤。 請讓我知道在 HttpInterceptor 中我們可以處理哪些類型的錯誤,以及在 ErrorHandler 中我們可以處理哪些錯誤。 我們需要兩者還是任何一個就足夠了

 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM