当我在 angular 8 中抛出错误时,未调用 Catch Error
Catch Error is not being called when i throw an error in angular 8
我添加了一个 HTTP 拦截器来捕获请求中所有可能的错误,然后使用 throwError
抛出该错误
constructor(private accountService: AccountService) { }
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// add authorization header with jwt token if available
const currentuser = this.accountService.isLoggedIn;
const token = localStorage.getItem('jwt');
if (currentuser && token !== undefined) {
request = request.clone({
setHeaders:
{
Authorization: `Bearer ${token}`
}
});
request = request.clone({ headers: request.headers.set('Content-Type', 'application/json') });
request = request.clone({ headers: request.headers.set('Accept', 'application/json') });
}
return next.handle(request).pipe(
catchError(error => {
debugger
if (error instanceof HttpErrorResponse) {
const unothorizedError = error;
if (unothorizedError.status === 401) {
if (unothorizedError.error != null) {
return throwError(unothorizedError.error);
}
}
if (unothorizedError.status === 0) {
return throwError('Connection failed to the server');
}
const applicationError = error.headers.get('Application-Error');
if (applicationError) {
return throwError(applicationError);
}
const serverError = error.error;
let modalStateErrors = '';
if (serverError && serverError.errors && typeof serverError.errors === 'object') {
for (const key in serverError.errors) {
if (serverError.errors[key]) {
modalStateErrors += serverError.errors[key] + '\n';
}
}
}
return throwError(modalStateErrors || serverError || 'Server Error');
}
})
);
}
我正在尝试从我的解析器中捕获错误,但是在调试时我注意到没有调用捕获错误
resolve(route: ActivatedRouteSnapshot): Observable<Product[]> {
debugger
return this.productsService
.getAllProducts(this.pageNumber, this.pageSize, null)
.pipe(
catchError(error => {
console.log(error);
// this.snackBar.open('Problem retriving your data', 'cancel', {
// duration: 5000 ,
// });
// this.router.navigate(['/home']);
return of(null);
})
);}
然后 TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
被抛出。
所以好像我无法捕获拦截器给出的错误。
我在这里看到这个问题 link 我添加了
else {return throwError(error);}
在 HTTP 拦截器中它可以工作,但我不知道为什么
我添加了一个 HTTP 拦截器来捕获请求中所有可能的错误,然后使用 throwError
抛出该错误 constructor(private accountService: AccountService) { }
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
// add authorization header with jwt token if available
const currentuser = this.accountService.isLoggedIn;
const token = localStorage.getItem('jwt');
if (currentuser && token !== undefined) {
request = request.clone({
setHeaders:
{
Authorization: `Bearer ${token}`
}
});
request = request.clone({ headers: request.headers.set('Content-Type', 'application/json') });
request = request.clone({ headers: request.headers.set('Accept', 'application/json') });
}
return next.handle(request).pipe(
catchError(error => {
debugger
if (error instanceof HttpErrorResponse) {
const unothorizedError = error;
if (unothorizedError.status === 401) {
if (unothorizedError.error != null) {
return throwError(unothorizedError.error);
}
}
if (unothorizedError.status === 0) {
return throwError('Connection failed to the server');
}
const applicationError = error.headers.get('Application-Error');
if (applicationError) {
return throwError(applicationError);
}
const serverError = error.error;
let modalStateErrors = '';
if (serverError && serverError.errors && typeof serverError.errors === 'object') {
for (const key in serverError.errors) {
if (serverError.errors[key]) {
modalStateErrors += serverError.errors[key] + '\n';
}
}
}
return throwError(modalStateErrors || serverError || 'Server Error');
}
})
);
}
我正在尝试从我的解析器中捕获错误,但是在调试时我注意到没有调用捕获错误
resolve(route: ActivatedRouteSnapshot): Observable<Product[]> {
debugger
return this.productsService
.getAllProducts(this.pageNumber, this.pageSize, null)
.pipe(
catchError(error => {
console.log(error);
// this.snackBar.open('Problem retriving your data', 'cancel', {
// duration: 5000 ,
// });
// this.router.navigate(['/home']);
return of(null);
})
);}
然后 TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.
被抛出。
所以好像我无法捕获拦截器给出的错误。
我在这里看到这个问题 link 我添加了
else {return throwError(error);}
在 HTTP 拦截器中它可以工作,但我不知道为什么