我已实施HttpInterceptor
,为每个请求添加标头,并在服务器使用401
回复时重定向到登录页面。我不希望它做任何其他事情。
HttpErrorResponse
。但是在我的控制器中,我只得到一个TypeError
对象而没有来自服务器的详细信息。
我附上相关代码。
拦截
@Injectable()
export class HttpAuthorizationService implements HttpInterceptor {
constructor(private cookieService: CookieService, private angularRouter: Router) { }
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
req = req.clone({
setHeaders: {
Authorization: 'Bearer ' + this.cookieService.get('Authorization')
}
});
return next.handle(req).do((ev: HttpEvent<any>) => {})
.catch(error => {
if (error instanceof HttpErrorResponse) {
if (error.status === 401) {
this.cookieService.set('expired', 'Session expired');
this.angularRouter.navigate(['/login']);
}
}
return Observable.throw(error); <-- i think this is bad
});
}
}
和实际控制器
deleteCategory(category) {
this.confirmationService.confirm({
message: 'Are you sure you want to delete?',
header: 'Delete product category ' + category,
icon: 'fa fa-trash',
accept: () => {
this.productService.deleteCategory(category).subscribe(
success => {
this.msgs = [{ severity: 'success', summary: 'Success', detail: 'Successfully deleted the product category ' + category }];
},
error => {
this.categories.push(category);
this.msgs = [{ severity: 'error', summary: 'Could not delete category', detail: error.status + '-' + error.error }]; <-- getting object without error attribute and no info from the server
}
);
},
reject: () => this.categories.push(category)
});
}
我的问题是如何保留原始错误对象而不是实际获得的内容 如果我没有实现这个HttpInterceptor比我的控制器获得正常的错误对象
答案 0 :(得分:0)
我不得不在我的HttpInterceptor实现中添加另一个导入
{
"rules": {
".read": true,
"room": {
"$roomID": {
"ready":{
".write": "data.parent().child('creatorUid').val() == auth.uid"
}
}
}
}
}