【发布时间】:2019-10-16 11:40:46
【问题描述】:
如果我得到代码“ERROR”,我会尝试在地图函数中返回错误。
import {Injectable} from '@angular/core';
import {HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse} from '@angular/common/http';
import {Observable} from 'rxjs';
import {map} from 'rxjs/operators';
@Injectable()
export class ResponseInterceptor implements HttpInterceptor {
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const request = req.clone();
return next.handle(request).pipe(
map((event: any) => {
if (event instanceof HttpResponse) {
if (event.body && event.body.code === 'ERROR') {
throw new Error('Fatal Error');
}
const body = event.body ? event.body.body || event.body : null;
if (body) {
return event.clone({body});
}
}
return event;
})
);
}
}
但我在控制台中发现错误:
core.js:15724 错误类型错误:您在预期流的位置提供了“未定义”。您可以提供 Observable、Promise、Array 或 Iterable。 在 subscribeTo (subscribeTo.js:41) 在 subscribeToResult (subscribeToResult.js:11)
【问题讨论】:
标签: angular typescript rxjs angular-http-interceptors