Search code examples
angularangular-httpclienthttp-status-code-429

Angular 6 - 429 response handling


How can you capture a 429 error response on the HttpClient in Angular 6?

The example below will correctly capture a 401 error but for the 429 it feeds back an unknown error.

Error/Edit: Failure occurs on the preflight OPTIONS check.

Login Code:

this.http.post<any>(this._loginUrl, this.loginUserData)
  .subscribe(
    res => {
      console.log(res)
    },
    err => {
      console.log(err);
      if (err.status == 401) {
        // unauthorized
      } else if (err.status == 429) {
        // limit reached
      } else {
        // unknown
      }
    }
  )

401 Response

HttpErrorResponse {headers: HttpHeaders, status: 401, statusText: "Unauthorized", url: "http://localhost:82/api/login", ok: false, …}

429 Response

HttpErrorResponse {headers: HttpHeaders, status: 0, statusText: "Unknown Error", url: null, ok: false, …}

Solution

  • Since your failure occurs on the preflight OPTIONS request, it most likely means that the response is missing the CORS headers (that's why youre getting a status of 0, rather than 429).