【发布时间】:2020-01-08 04:25:05
【问题描述】:
所以当前端有错误请求时,后端Server返回不同的状态码和HttpErrorResponse;我已经意识到管理这个问题的最佳方法是在 Ionic 4/Angular 7 设置中使用拦截器。
我已经尝试过几次拦截器,但遇到了不同的问题。我现在正在按照link 中的步骤进行操作
我的服务是这样定义的:
saveLocationNew(request: AddLocationRequest, entityId:string, userId:string): Observable<AddLocationResponse> {
return this.httpClient.post<AddLocationResponse>(this.addLocationUrl, request , {headers:Constants.getHeaders(userId,entityId)})
}
现在有了拦截器:
saveLocationNew(request: AddLocationRequest, entityId:string, userId:string): Observable<AddLocationResponse> {
return this.httpClient.post<AddLocationResponse>(this.addLocationUrl, request , {headers:Constants.getHeaders(userId,entityId)})
.pipe(
tap(_ => console.log('creating a new location')),
catchError(this.handleError('new Location',[]))
);
}
问题是我现有的服务返回了 AddLocationResponse 类型的响应;但是如果出现错误,我如何定义返回对象的类型,离子服务也会抛出这个错误:
ERROR in src/app/services/location.service.ts(42,11): error TS2322: Type 'Observable<any[] | AddLocationResponse>' is not assignable to type 'Observable<AddLocationResponse>'.
[ng] Type 'any[] | AddLocationResponse' is not assignable to type 'AddLocationResponse'.
[ng] Type 'any[]' is not assignable to type 'AddLocationResponse'.
[ng] Property 'name' is missing in type 'any[]'.
[ng] src/app/services/location.service.ts(74,49): error TS2339: Property 'message' does not exist on type 'T'.
知道实现拦截器的最佳方法是什么,这样组件(*.page.ts 文件)就不需要任何更改。
我的拦截器看起来像这样:
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const token = localStorage.getItem('token');
if (token) {
request = request.clone({
setHeaders: {
'Authorization': token
}
});
}
if (!request.headers.has('Content-Type')) {
request = request.clone({
setHeaders: {
'content-type': 'application/json'
}
});
}
request = request.clone({
headers: request.headers.set('Accept', 'application/json')
});
return next.handle(request).pipe(
map((event: HttpEvent<any>) => {
if (event instanceof HttpResponse) {
console.log('event--->>>', event);
}
return event;
}),
catchError((error: HttpErrorResponse) => {
if (error.status === 401) {
if (error.error.success === false) {
this.presentToast('Login failed');
} else {
console.log(' route this to right place');
// this.router.navigate(['auth/login']);
}
}
if (error.status === 500) {
if (error.error.success === false) {
this.presentToast('Something went wrong, Please contact Administrator');
} else {
console.log(' route this to right place');
// this.router.navigate(['auth/login']);
}
}
//431
if (error.status === 431) {
if (error.error.success === false) {
this.presentToast('Something went wrong with the HttpRequest, Please contact Administrator');
} else {
console.log(' route this to right place');
// this.router.navigate(['auth/login']);
}
}
// add all the other error codes here
return throwError(error);
}));
}
我不确定还需要添加/修改什么,以便拦截 HttpErrorResponse。
【问题讨论】:
-
你使用了错误的拦截器。您应该使用拦截器来捕获所有或大部分请求的错误,所有这些都来自一个地方。
-
@Dino 进行了更改,但代码 431 和 500 出现错误我可以看到它们仍未被拦截器处理
标签: angular angular7 ionic4 angular-http-interceptors