这曾经工作得很好,但 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);
})
)
}
}
非常感谢您的帮助。
非常感谢
好的,我排序了,返回 throwError(() => error.message);它返回一个对象并寻找一个字符串。