Angular 7 Error handling

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.



ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..
My Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.
^