Angular HttpInterceptor修改错误object
Angular HttpInterceptor modifies the error object
我已经实现了一个 HttpInterceptor
,它向每个请求添加一个 header,如果服务器以 [=15=] 响应,则重定向到登录页面。我不希望它做任何其他事情。
但是我注意到,每当我从服务器收到错误响应(如我所料)时,拦截器都会踢它并抛出 HttpErrorResponse
。但是在我的控制器中,我只得到一个 TypeError
object 而没有来自服务器的详细信息。
我附上相关代码。
拦截器
@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)
});
}
我的问题是如何保留原始错误 object 而不是我实际得到的错误
如果我不实现这个 HttpInterceptor,我的控制器就会得到正常的错误 object
我不得不向我的 HttpInterceptor 实现添加另一个导入
import 'rxjs/add/observable/throw';
我已经实现了一个 HttpInterceptor
,它向每个请求添加一个 header,如果服务器以 [=15=] 响应,则重定向到登录页面。我不希望它做任何其他事情。
但是我注意到,每当我从服务器收到错误响应(如我所料)时,拦截器都会踢它并抛出 HttpErrorResponse
。但是在我的控制器中,我只得到一个 TypeError
object 而没有来自服务器的详细信息。
我附上相关代码。
拦截器
@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)
});
}
我的问题是如何保留原始错误 object 而不是我实际得到的错误 如果我不实现这个 HttpInterceptor,我的控制器就会得到正常的错误 object
我不得不向我的 HttpInterceptor 实现添加另一个导入
import 'rxjs/add/observable/throw';