angular - 使用 Angular 7 处理 HTTPErrorResponse 的最佳方式

标签 angular angular7 ionic4 angular-http-interceptors

所以当前端有错误请求时,后端Server会返回不同的状态码和HttpErrorResponse;我已经意识到最好的管理方法是在 Ionic 4/Angular 7 设置中使用拦截器。

我已经尝试过几次拦截器,但都遇到了不同的问题。我现在正在执行此 link 中的步骤

我的服务是这样定义的:

     saveLocationNew(request: AddLocationRequest, entityId:string, userId:string): Observable<AddLocationResponse> {

          return this.httpClient.post<AddLocationResponse>(this.addLocationUrl, request ,  {headers:Constants.getHeaders(userId,entityId)})
       }

现在有了拦截器:

    saveLocationNew(request: AddLocationRequest, entityId:string, userId:string): Observable<AddLocationResponse> {

      return this.httpClient.post<AddLocationResponse>(this.addLocationUrl, request ,  {headers:Constants.getHeaders(userId,entityId)})
      .pipe(
    tap(_ => console.log('creating a new location')),
    catchError(this.handleError('new Location',[]))
  );  
}

问题是我现有的服务返回了 AddLocationResponse 类型的响应;但是如果出现错误,我该如何定义返回对象的类型,并且 ionic serve 也会抛出此错误:

    ERROR in src/app/services/location.service.ts(42,11): error TS2322: Type 'Observable<any[] | AddLocationResponse>' is not assignable to type 'Observable<AddLocationResponse>'.
[ng]   Type 'any[] | AddLocationResponse' is not assignable to type 'AddLocationResponse'.
[ng]     Type 'any[]' is not assignable to type 'AddLocationResponse'.
[ng]       Property 'name' is missing in type 'any[]'.
[ng] src/app/services/location.service.ts(74,49): error TS2339: Property 'message' does not exist on type 'T'.

知道什么是实现拦截器的最佳方式,这样组件(*.page.ts 文件)就不需要任何更改了。

我的拦截器是这样的:

  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

  const token = localStorage.getItem('token');

  if (token) {
    request = request.clone({
      setHeaders: {
        'Authorization': token
      }
    });
  }

  if (!request.headers.has('Content-Type')) {
    request = request.clone({
      setHeaders: {
        'content-type': 'application/json'
      }
    });
  }

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

  return next.handle(request).pipe(
    map((event: HttpEvent<any>) => {
      if (event instanceof HttpResponse) {
        console.log('event--->>>', event);
      }
      return event;
    }),
    catchError((error: HttpErrorResponse) => {
      if (error.status === 401) {
        if (error.error.success === false) {
          this.presentToast('Login failed');
        } else {
            console.log(' route this to right place');
        //   this.router.navigate(['auth/login']);
        }
      }
          if (error.status === 500) {
        if (error.error.success === false) {
          this.presentToast('Something went wrong, Please contact Administrator');
        } else {
            console.log(' route this to right place');
        //   this.router.navigate(['auth/login']);
        }
      }
      //431

       if (error.status === 431) {
        if (error.error.success === false) {
          this.presentToast('Something went wrong with the HttpRequest, Please contact Administrator');
        } else {
            console.log(' route this to right place');
        //   this.router.navigate(['auth/login']);
        }
      }

      // add all the other error codes here 
      return throwError(error);
    }));
}

我不确定还需要添加/修改什么才能拦截 HttpErrorResponse。

最佳答案

你可以这样使用它

    saveLocationNew(request: AddLocationRequest, entityId:string, userId:string): Observable<AddLocationResponse> {

      return this.httpClient.post<AddLocationResponse>(this.addLocationUrl, request ,  {headers:Constants.getHeaders(userId,entityId)})
      .pipe(
    tap(_ => console.log('creating a new location')),
    catchError(this.handleError<string>('stats')))
  )
}

现在创建名为 handle error 的函数来处理所有这样的错误请求

private handleError<T>(operation = 'operation', result?: T) {
    return (error: any): Observable<T> => {
      console.error(error);   
      console.log(`${operation} failed: ${error.message}`);
      if(error.error.message == "Token is invalid!"){
        localStorage.removeItem('token');
        this.router.navigateByUrl('/login');
      }
      else{
        return throwError(error)
      }
      return of(result as T);
    };
  }

如果您想在组件中处理该错误,则需要抛出该错误。希望这对您有所帮助。

关于angular - 使用 Angular 7 处理 HTTPErrorResponse 的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57802568/

相关文章:

angular - 在 Angular 5 应用程序中导入第三方 js 库 cq-prolyfill 的正确方法

javascript - 原始异常: Cannot read property 'post' of undefined in angular 2

ionic4 - 如何为用户提供设置 baseurl 的选项?

angular - Ionic 4每次进入都会刷新标签页

angular - 缺少必需的获取参数 Angular 4

javascript - 如何从数据库中显示所需格式的日期,从iso8601到类型="date"

Angular 7 - 如何分隔不同目录中的文件

css - ng-bootstrap Carousel 在全窗口中看起来不正确,但至少看起来不错

css - 在 Angular-material 中将 more_vert 按钮对齐右侧

html - intro.js 中是否有任何选项可以使突出显示的文本或图像清晰