簡體   English   中英

如何處理響應對象中的錯誤

[英]How to Handle Errors in response object

在使用錯誤的用戶名或密碼進行日志記錄時,它會向我發送一個錯誤對象作為響應

POST https://abcdserver.zxcv.com/abcdserver/connect/token 400 (Bad Request)
Error performing password flow 
HttpErrorResponse {headers: HttpHeaders, status: 400, statusText: "Bad Request", url: "https://abcdserver.zxcv.com/abcdserver/connect/token", ok: false, …}

如何處理這些錯誤。 這是我的代碼

login.component.ts

jwtHelper: JwtHelper = new JwtHelper();
  this.authService.loginOrg(this.userCredential.email, this.userCredential.password).then((resp) => {
    let token = resp['access_token'];
    if (token === null) {
      this.invalidLogin = true;
    }
    let decodedToken = this.jwtHelper.decodeToken(token);
    this.UserId = decodedToken.userid;
  }, (reason) => {
    this.invalidLogin = true;
  });

authservice.component.ts

import { OAuthService } from 'angular-oauth2-oidc';    
public loginOrg(userName: string, password: string) {
        return this.oAuthService.fetchTokenUsingPasswordFlow(userName, password);
    }

有兩種選擇:
1)實現ErrorHandler( https://angular.io/api/core/ErrorHandler )來處理應用程序中生成的所有錯誤,或者
2)處理你的http調用中的每個錯誤,如下所示:
this.getMEthod()。subscribe(resp => {your logic here},error => {your logic here})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM