gpt4 book ai didi

angular - 处理ErrorHandler中的错误和HttpInterceptor中的处理

转载 作者:行者123 更新时间:2023-12-03 08:43:23 24 4
gpt4 key购买 nike

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);
}
}
}

最佳答案

还有一个类似的问题here,也许是您想要的东西! :)

关于angular - 处理ErrorHandler中的错误和HttpInterceptor中的处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59662935/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com