当我在角度8中抛出错误时,未调用捕获错误

问题描述 投票:0回答:1

我添加了一个HTTP拦截器来捕获请求中所有可能的错误,然后使用throwError抛出该错误

    constructor(private accountService: AccountService) { }

    intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
        // add authorization header with jwt token if available
        const currentuser = this.accountService.isLoggedIn;
        const token = localStorage.getItem('jwt');
        if (currentuser && token !== undefined) {
            request = request.clone({
                setHeaders:
                {
                    Authorization: `Bearer ${token}`

                }
            });
            request = request.clone({ headers: request.headers.set('Content-Type', 'application/json') });

            request = request.clone({ headers: request.headers.set('Accept', 'application/json') });
        }

        return next.handle(request).pipe(
            catchError(error => {
                debugger
                if (error instanceof HttpErrorResponse) {

                    const unothorizedError = error;

                    if (unothorizedError.status === 401) {
                        if (unothorizedError.error != null) {
                            return throwError(unothorizedError.error);
                        }
                    }

                    if (unothorizedError.status === 0) {
                        return throwError('Connection failed to the server');
                      }

                    const applicationError = error.headers.get('Application-Error');

                    if (applicationError) {
                        return throwError(applicationError);
                    }

                    const serverError = error.error;
                    let modalStateErrors = '';

                    if (serverError && serverError.errors && typeof serverError.errors === 'object') {
                        for (const key in serverError.errors) {
                            if (serverError.errors[key]) {
                                modalStateErrors += serverError.errors[key] + '\n';
                            }
                        }
                    }
                    return throwError(modalStateErrors || serverError || 'Server Error');
                }
            })
        );
    }

而且我正在尝试从解析器捕获错误,但是在调试时,我注意到没有调用catch错误

    resolve(route: ActivatedRouteSnapshot): Observable<Product[]> {
    debugger
    return this.productsService
      .getAllProducts(this.pageNumber, this.pageSize, null)
      .pipe(
        catchError(error => {
          console.log(error);
          // this.snackBar.open('Problem retriving your data', 'cancel', {
          //   duration: 5000 ,
          // });
          // this.router.navigate(['/home']);
          return of(null);
        })
      );}

然后抛出TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.。>>

所以就像我无法捕捉到拦截器给出的错误。

我添加了一个HTTP拦截器来捕获请求中所有可能的错误,然后使用throwError构造函数(private accountService:AccountService){}

我在这里link中看到了这个问题,我添加了
else {return throwError(error);}
在HTTP拦截器中,它可以工作,但我不知道为什么
angular typescript interceptor
1个回答
0
投票
在HTTP拦截器中,它可以工作,但我不知道为什么
© www.soinside.com 2019 - 2024. All rights reserved.