Angular HttpInterceptor重构错误消息

Angular HttpInterceptor重构错误消息,angular,http,angular-http-interceptors,Angular,Http,Angular Http Interceptors,我有一个HttpInterceptor,我希望它重构我的错误以删除括号 @Injectable() export class AuthInterceptor implements HttpInterceptor { constructor(public auth: AuthProvider, public app: App, private global: GlobalVariablesProvider) {} intercept(request: HttpRequest<an

我有一个HttpInterceptor,我希望它重构我的错误以删除括号

@Injectable()
export class AuthInterceptor implements HttpInterceptor {

  constructor(public auth: AuthProvider, public app: App, private global: GlobalVariablesProvider) {}

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

    return next.handle(request).do((event: HttpEvent<any>) => {
      if (event instanceof HttpResponse) {

      }
    }, (err: any) => {
      // modify err here?
    });
  }
}

在拦截器和HTTP中,将使用句柄(请求)捕获错误。捕获(…)

您可以做的是:

@Injectable()
export class AuthInterceptor implements HttpInterceptor {

    constructor(public auth: AuthProvider, public app: App, private global: GlobalVariablesProvider) {}

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

        return next.handle(request)
            .do((event: HttpEvent<any>) => {
                if (event instanceof HttpResponse) {
                    // do what you want here if your have to
                }
            })
            .catch((err: any) => {
                if (err && err.error) {
                    err.error = err.error.replace(/\[|]/, '');
                }
                return Observable.throw(err);
            });
    }
}
@Injectable()
导出类AuthInterceptor实现HttpInterceptor{
构造函数(public auth:AuthProvider、public app:app、private global:GlobalVariablesProvider){}
拦截(请求:HttpRequest,下一步:HttpHandler):可观察{
返回next.handle(请求)
.do((事件:HttpEvent)=>{
if(HttpResponse的事件实例){
//如果有必要,在这里做你想做的事
}
})
.catch((错误:任意)=>{
if(err&&err.error){
err.error=err.error.replace(/\[|]/,“”);
}
返回可观察。抛出(错误);
});
}
}

这可能会有所帮助,请注意
.flatMap()
可观察。create()
是否需要使用
.catch()
而不是onError位置回调来
返回
?拦截器方法返回可观察对象,而不是观察者。当服务器出现http错误时,我认为捕获它的唯一方法是使用catch操作符。但是maibe im错误地从我的错误拦截器的实现重定向到登录,当我们得到
403
时,我使用位置回调,就像在OP的帖子中一样。我只是好奇它们在功能上是否等效,因为我从未尝试过
.catch()
@Injectable()
export class AuthInterceptor implements HttpInterceptor {

    constructor(public auth: AuthProvider, public app: App, private global: GlobalVariablesProvider) {}

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

        return next.handle(request)
            .do((event: HttpEvent<any>) => {
                if (event instanceof HttpResponse) {
                    // do what you want here if your have to
                }
            })
            .catch((err: any) => {
                if (err && err.error) {
                    err.error = err.error.replace(/\[|]/, '');
                }
                return Observable.throw(err);
            });
    }
}