首页 > 解决方案 > 如何阻止代码流进入Angular 8中的全局错误处理程序

问题描述

我在一个组件中特别需要处理组件本身的错误,但我的项目中也有全局错误处理程序拦截器,所以基本上我希望代码执行在 if 条件下停止,而不是进入错误处理程序。

API 响应返回“状态码:400[Bad request] ”。

下面是我在组件中的代码。

if (error.status === 400) {
  this.errorMessage = 'Passcode invalid. Please try again.';
  // I want my code to stop here from going into global error handler.
} else if (error.status === 403) {
  this.errorMessage = 'You do not have access to this feature. Please contact your system 
  administrator to proceed.';
} else if (error.status === 500 || error.status === 503) {
  this.errorMessage = 'Something went wrong,We are looking to fix it.';
}
this.showError = true;

这是错误处理程序服务中的功能:

handleError(response: HttpErrorResponse) {
  if (response.status === 403) {
    this.permissionDenied();
  } else if (response.status === 500 || response.status === 503) {
    this.serviceUnavailable();
  } else {
    // code goes here and executes below, which i don't want for that specific component.
    this.authTokenService.redirectToLogin();
  }
}

我附上了一张到底发生了什么的图片:

无效密码错误

出现此消息,显然表明 if 条件执行,但用户在此之后立即注销。那就是问题所在。

标签: angulartypescriptangular-http-interceptors

解决方案


推荐阅读