Handling API Responses and Errors

Tutorial 2 of 5

1. Introduction

This tutorial aims to guide you through handling API responses and errors in Angular. By the end of this tutorial, you will be capable of managing successful API calls and know how to effectively deal with any errors that might occur during these calls.

In this tutorial, you will learn:
- How to use the subscribe() method to handle successful API responses.
- How to use the catch() method to handle errors in API calls.

Prerequisites:
Before starting this tutorial, it is helpful to have a basic understanding of Angular and TypeScript. Familiarity with HTTP requests and APIs would also be beneficial.

2. Step-by-Step Guide

Angular provides a powerful HTTP client library that makes it easy to communicate with API servers. This library returns an Observable that you can subscribe to get the response from the API call.

Handling Successful API Calls with subscribe()

The subscribe() method is used to handle successful API calls. It takes up to three arguments: the success handler, the error handler, and a completion handler.

Handling Errors with catch()

The catch() method is used to handle errors while making API calls. If an API call fails, you can use catch() to catch the error and take appropriate action.

3. Code Examples

Let's take a look at a practical example of making an API call and handling the response and errors.

import { HttpClient } from '@angular/common/http';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/throw';

constructor(private http: HttpClient) { }

getPosts() {
  this.http.get('http://jsonplaceholder.typicode.com/posts')
    .subscribe(
      data => console.log(data), // Success handler
      error => console.error(error)  // Error handler
    );
}

In the above code:
- We import HttpClient and use it to make the GET request to the API.
- We subscribe to the Observable returned by the get() method.
- If the API call is successful, the data is logged to the console.
- If there's an error, it's caught and logged to the console as well.

4. Summary

In this tutorial, you've learned how to handle API responses and errors in Angular. You've learned how to use the subscribe() method to handle successful API responses and how to use the catch() method to handle errors.

5. Practice Exercises

  1. Create a service that makes a POST request to an API and handles the response and any potential errors.
  2. Expand the previous exercise to retry the request if it fails.
  3. Create a service that makes multiple API requests and handles the responses and any potential errors.

Solutions:

this.http.post('http://jsonplaceholder.typicode.com/posts', {title: 'Test'})
  .subscribe(
    data => console.log(data),
    error => console.error(error)
  );
this.http.post('http://jsonplaceholder.typicode.com/posts', {title: 'Test'})
  .retry(3)
  .subscribe(
    data => console.log(data),
    error => console.error(error)
  );
Observable.forkJoin(
  this.http.get('http://jsonplaceholder.typicode.com/posts'),
  this.http.get('http://jsonplaceholder.typicode.com/comments')
).subscribe(
  data => console.log(data),
  error => console.error(error)
);

Keep practicing and experimenting with different API calls and error situations to get a better grip on handling responses and errors. Happy coding!