Skip to main content

Angular 8 Error handling


If the Angular request fails on the server due to network issues or anything bad happen in your apps then HttpClient will return an error object instead of a successful response. If getting an error or fails your request, you just need to handle this error in the component by passing the error object as a second callback to subscribe () method.

Let's see an example -
  //fetch courses data
  fetchCourses() {
      this.coursesService.getCourses()
      .subscribe(
        (data: Courses) => this.courses = { …data }, // If success
         error => this.error = error // If got an error
      );
  }

It’s always a good idea to give meaningful feedback instead of displaying the error object returned from HttpClient.