Angular 如何通过HttpInterceptor映射HttpResponseError值?
我在nodejs中对nodejs使用Angular;我正在以自定义格式包装错误,如: server/routes/data.route.js:Angular 如何通过HttpInterceptor映射HttpResponseError值?,angular,rxjs,Angular,Rxjs,我在nodejs中对nodejs使用Angular;我正在以自定义格式包装错误,如: server/routes/data.route.js: router.post('/data', (req, res) => { getData().then(data => res.send(data)) .catch(errObj => res.status(errObj .status).send(errObj )); }); 此errObj和我从
router.post('/data', (req, res) => {
getData().then(data => res.send(data))
.catch(errObj => res.status(errObj .status).send(errObj ));
});
此errObj和我从节点返回的每个错误值将采用以下格式:
{
title: 'cannot update document',
control: 'name',
status: 406,
desc: 'new doc is invalid name'
}
因此,在subscribe
中的每个错误回调中,角度;我希望errObj在HttpErrorResponse
中出现,比如:
// inside intercept function
return next.handle(req.clone()).pipe(
map(res => {
console.log(res);
return (res instanceof HttpErrorResponse)? res.error: res;
})
);
src/app/app.component.ts
this.service.create(document).subscribe(
res => { ... },
error => {
// I have to rewrite this in each error callback
let errObj = error.error;
// do something
}
)
@Injectable()
export class ErrorInterceptor implements HttpInterceptor {
constructor(private authenticationService: AuthenticationService) {}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(catchError(err => {
if (err.status === 406) {
// do something
}
const error = err.error || err.statusText;
return throwError(error);
}))
}
}
因此,我想使用rxjs/operators
的map
直接映射来自HttpInterceptor
的错误,如:
// inside intercept function
return next.handle(req.clone()).pipe(
map(res => {
console.log(res);
return (res instanceof HttpErrorResponse)? res.error: res;
})
);
每次我在map中记录res时,它都会打印:{type:0}
,显然它不起作用
如何映射来自
HttpInterceptor
的响应?创建错误侦听器,例如:
错误.interceptor.ts
this.service.create(document).subscribe(
res => { ... },
error => {
// I have to rewrite this in each error callback
let errObj = error.error;
// do something
}
)
@Injectable()
export class ErrorInterceptor implements HttpInterceptor {
constructor(private authenticationService: AuthenticationService) {}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(catchError(err => {
if (err.status === 406) {
// do something
}
const error = err.error || err.statusText;
return throwError(error);
}))
}
}
@Injectable()
导出类ErrorInterceptor实现HttpInterceptor{
构造函数(私有authenticationService:authenticationService){}
拦截(请求:HttpRequest,下一步:HttpHandler):可观察{
返回next.handle(request).pipe(catchError(err=>{
如果(错误状态===406){
//做点什么
}
常量错误=err.error | | err.statusText;
返回投掷器(错误);
}))
}
}
创建错误拦截器,例如:
错误.interceptor.ts
this.service.create(document).subscribe(
res => { ... },
error => {
// I have to rewrite this in each error callback
let errObj = error.error;
// do something
}
)
@Injectable()
export class ErrorInterceptor implements HttpInterceptor {
constructor(private authenticationService: AuthenticationService) {}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(catchError(err => {
if (err.status === 406) {
// do something
}
const error = err.error || err.statusText;
return throwError(error);
}))
}
}
@Injectable()
导出类ErrorInterceptor实现HttpInterceptor{
构造函数(私有authenticationService:authenticationService){}
拦截(请求:HttpRequest,下一步:HttpHandler):可观察{
返回next.handle(request).pipe(catchError(err=>{
如果(错误状态===406){
//做点什么
}
常量错误=err.error | | err.statusText;
返回投掷器(错误);
}))
}
}
您能试试这个,并用日志更新我吗
return next.handle(req.clone()).pipe(
return next.handle(req).pipe(
tap(res=> {
if (res instanceof HttpResponse) {
console.log('We have response'); // Please look at this first
console.log(res); // Please look at this first
}
}),
catchError((err: any) => {
console.log('We have an error here')
return (err instanceof HttpErrorResponse)? err.error: of(err);
}));
}
);
你能试试这个,并用日志更新我吗
return next.handle(req.clone()).pipe(
return next.handle(req).pipe(
tap(res=> {
if (res instanceof HttpResponse) {
console.log('We have response'); // Please look at this first
console.log(res); // Please look at this first
}
}),
catchError((err: any) => {
console.log('We have an error here')
return (err instanceof HttpErrorResponse)? err.error: of(err);
}));
}
);
尝试捕获拦截器中的错误,并使用转换后的值返回observable
按照这样做的方式,您只需将错误映射并传播到下一个处理程序 尝试捕获拦截器中的错误,并使用转换后的值返回observable
按照这样做的方式,您只需将错误映射并传播到下一个处理程序 如果你查看http响应状态,这是一个错误的请求响应吗?@yousseftounsi我知道,我想在错误回调中捕捉它!不只是问,你确定nodeJs返回的是错误的请求HTTP状态吗?是的,因为我使用了tap函数,它会按预期打印错误。如果你查看响应HTTP状态,这是错误的请求响应吗?@Yousseftounsi我知道,我想在错误回调中捕捉它!不只是问,你确定nodeJs返回了一个错误的请求HTTP状态吗?是的,因为我使用了tap函数,它按预期打印错误。@nimeresam你检查了吗?@nimeresam你检查了吗?