Angular:针对特定情况进行不同处理的 HttpInterceptor
Angular: HttpInterceptor with different handling for specific situation
我知道 Angular HTTPInterceptor:
@Injectable()
export class HTTPRequestInterceptor implements HttpInterceptor {
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(
tap(
(_response: HttpEvent<any>) => {
// nothing to do ... ... used for sending anyway...
},
(error: HttpErrorResponse) => {
... // display message
}
据我了解,这适用于我的所有请求。现在我的情况是,对于一个特定的请求,我不想应用常规的错误处理。有没有一种简单的方法可以实现这一目标?例如在我的请求中设置一些特殊参数
this.http.get<MyInfo>(`getlastInfo/${id}`).toPromise();
这可能是一种脆弱的方法,但一种方法可能是检查拦截器中的 URL 并有条件地执行拦截器中的逻辑。
@Injectable()
export class HTTPRequestInterceptor implements HttpInterceptor {
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(
tap(
(_response: HttpEvent<any>) => {
// nothing to do ... ... used for sending anyway...
},
(error: HttpErrorResponse) => {
if(req.url === "some_url"){
//handle error for specific case
}else{
//handle error for generic case
}
}
)
);
}
一种简单但肮脏的方式
@Injectable()
export class HTTPRequestInterceptor implements HttpInterceptor {
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if(req.url.contains('getlastInfo')){
//specific handling
} else {
//normal handling
}
}
您还可以使用 req.body
或 req.headers
进行检查,方法是在 header.
中设置特定键值等
正如 BELLIL 所建议的,阅读 header 比依赖 url 更容易。您仍然可以在将 header 发送到服务器之前将其删除:
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const doSpecificHandling = req.headers.has('Angular-Error-Handler');
return next.handle(req.clone({
headers: req.headers.delete('Angular-Error-Handler')
})).pipe(
tap(
(_response: HttpEvent<any>) => {
// nothing to do ... ... used for sending anyway...
},
(error: HttpErrorResponse) => {
if (doSpecificHandling) {
//handle error for specific case
} else {
//handle error for generic case
}
}
)
);
}
随着请求:
this.http.get<MyInfo>(`getlastInfo/${id}`, {
headers: new HttpHeaders().set('Angular-Error-Handler', 'diff'),
}).toPromise();
我知道 Angular HTTPInterceptor:
@Injectable()
export class HTTPRequestInterceptor implements HttpInterceptor {
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(
tap(
(_response: HttpEvent<any>) => {
// nothing to do ... ... used for sending anyway...
},
(error: HttpErrorResponse) => {
... // display message
}
据我了解,这适用于我的所有请求。现在我的情况是,对于一个特定的请求,我不想应用常规的错误处理。有没有一种简单的方法可以实现这一目标?例如在我的请求中设置一些特殊参数
this.http.get<MyInfo>(`getlastInfo/${id}`).toPromise();
这可能是一种脆弱的方法,但一种方法可能是检查拦截器中的 URL 并有条件地执行拦截器中的逻辑。
@Injectable()
export class HTTPRequestInterceptor implements HttpInterceptor {
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(
tap(
(_response: HttpEvent<any>) => {
// nothing to do ... ... used for sending anyway...
},
(error: HttpErrorResponse) => {
if(req.url === "some_url"){
//handle error for specific case
}else{
//handle error for generic case
}
}
)
);
}
一种简单但肮脏的方式
@Injectable()
export class HTTPRequestInterceptor implements HttpInterceptor {
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if(req.url.contains('getlastInfo')){
//specific handling
} else {
//normal handling
}
}
您还可以使用 req.body
或 req.headers
进行检查,方法是在 header.
正如 BELLIL 所建议的,阅读 header 比依赖 url 更容易。您仍然可以在将 header 发送到服务器之前将其删除:
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const doSpecificHandling = req.headers.has('Angular-Error-Handler');
return next.handle(req.clone({
headers: req.headers.delete('Angular-Error-Handler')
})).pipe(
tap(
(_response: HttpEvent<any>) => {
// nothing to do ... ... used for sending anyway...
},
(error: HttpErrorResponse) => {
if (doSpecificHandling) {
//handle error for specific case
} else {
//handle error for generic case
}
}
)
);
}
随着请求:
this.http.get<MyInfo>(`getlastInfo/${id}`, {
headers: new HttpHeaders().set('Angular-Error-Handler', 'diff'),
}).toPromise();