捕获无法与httpclient正常工作

时间:2019-08-08 14:23:34

标签: angular error-handling httpclient

当Web服务不可用时,我正在尝试处理错误。
我遇到的问题是似乎在错误处理程序之前发生了“ return”语句。

当我运行该应用程序时,将对dosUserExist进行调用,但是在组件中返回了返回状态 并且showError在控制台显示错误消息之前执行。

在我的服务中,我有:

// Error Handler
private handleError(error: HttpErrorResponse) {
    if (error.error instanceof ErrorEvent) {
      // A client-side or network error occurred. Handle it accordingly.
      console.error('An error occurred:', error.error.message);
    } else {
      // The backend returned an unsuccessful response code.
      // The response body may contain clues as to what went wrong,
      console.error(
        `Backend returned code ${error.status}, ` +
        `body was: ${error.error}`);
    }
    // return an observable with a user-facing error message
    return throwError(
      'Something bad happened; please try again later.');
  }


// Webservice call to see if the user exists
doesUserExist(User: IUser) {


    const  params = new  HttpParams().set('userID', user.userID.ToString(););

    return this.httpClient.get<boolean>(this.urlBase + this.urlDoesNewUserExist, {params})
                          .toPromise()
                          .then(res => res as boolean)
                          .catch(this.handleError);
}

在组件中,我有:

if (await this.doesUserExist(this.user)) {
          // Create the strings that will be displayed by the Error dialog
          const errTitle = 'Error with User data.';
          const errMessage = 'A User with the specified First Name, Last Name, Phone Number and Drivers License Number' +
                              ' already exists in the Users database table.';
          const errDetail = 'Please see the attendant at the front desk.';

          // Show the user the error message
          this.showError(errTitle, errMessage, errDetail);

          // Exit this method
          return;

0 个答案:

没有答案