我有一个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?
});
}
}
我似乎无法直接修改err
,而且我不确定如何在修改错误后传递错误。我想对err
做出的修改是:
err.error.replace(/\[|]/, '')
答案 0 :(得分:2)
在拦截器中,HTTP错误将被句柄(请求).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);
});
}
}