Ionic 如何让catchError返回网络错误?

fivyi3re  于 12个月前  发布在  Ionic
关注(0)|答案(1)|浏览(92)

这曾经工作得很好,但throwError被弃用了,现在看起来catchError不再返回我曾经收到的网络错误。

this.goToApi().subscribe(response => {

  console.log(response);

},
err => {

  console.log(err);

});


public goToApi(): any
{
  return this.apiService.makeApiCall().pipe(map((response: any) => {

    return response;
  }),
    catchError((err) => { console.log(err); return err; })
  )
}

字符串
只是为了补充,这个拦截器是在我的API服务和它的显示错误罚款在那里.

import {
  HttpEvent,
  HttpInterceptor,
  HttpHandler,
  HttpRequest,
  HttpErrorResponse
 } from '@angular/common/http';
 import { Observable, throwError } from 'rxjs';
 import { retry, catchError } from 'rxjs/operators';
 import{ CustomError } from '../../utils/custom-error';
import { Injectable } from "@angular/core";

 @Injectable()
export class HttpErrorInterceptor implements HttpInterceptor {

  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(request)
      .pipe(
        retry(1),

        catchError((error: HttpErrorResponse) => {
          
          if (!window.navigator.onLine) {

            return throwError(() => new CustomError('Check your internet connection.'));
          }

          return throwError(() => error);
        })
      )
  }

 }


非常感谢你的帮助
非常感谢

x6492ojm

x6492ojm1#

我把它排序了,返回throwError(()=> error.message);它返回一个对象并查找一个字符串。

相关问题