【发布时间】:2021-08-03 21:30:51
【问题描述】:
我的 Angular 11 应用程序有一个烦人的问题:当我在我的语言环境机器上执行以下代码时,一切都很好,我的 toastrService 会显示正确的错误消息。但是当它被部署到 Azure 时,我总是得到 出了点问题!请再试一次!这是我的错误消息,用于与 HttpErrorResponse 不同的错误(检查组件代码以获取显示的错误消息)。
我有以下拦截方法:
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req).pipe(
catchError(err => {
// onError
console.log(err);
if (err instanceof HttpErrorResponse) {
console.log(err.status);
console.log(err.statusText);
if (err.status === 401) {
this.LogoutFromApplication();
}
}
return throwError(err);
}));
}
以及以下组件逻辑:
this.httpService.post(url, body)
.subscribe((result) => {
...
},
(errorResponse) => {
this.operationFlag = false;
console.log(JSON.stringify(errorResponse));
if (errorResponse.error && errorResponse.error.ErrorMessage) {
this.toastr.showError(errorResponse.error.ErrorMessage);
} else {
this.toastr.showError("Something went wrong! Please try again!");
}
});
不知何故,错误被拦截器吞没了。拦截器中的 console.log-s 显示错误存在并且它是一个 HttpErrorResponse 但在组件中记录了一个空对象并且 errorResponse 对象似乎未定义。
你能帮我找出问题所在吗?
【问题讨论】:
-
控制台输出的日志只是errorResponse给出了什么,未定义?
-
它会抛出一个错误。与此同时,我怀疑根本原因。我们正在使用 adal-angular5 的 Adal5HTTPService,我认为这可能是吞下错误的原因。如果我得到确认,我会更新主题。
标签: angular error-handling interceptor