Hone logo
Hone
Problems

Robust Error Handling in an Angular Service

Angular applications often interact with external APIs or perform operations that can fail. Implementing robust error handling is crucial for providing a good user experience and preventing unexpected application crashes. This challenge asks you to build an Angular service that fetches data from a mock API and gracefully handles potential errors, displaying informative messages to the user.

Problem Description

You need to create an Angular service called DataService that fetches user data from a mock API endpoint. The API might return successful responses, network errors, or server-side errors (simulated by occasionally returning a 500 status code). Your service should:

  1. Fetch Data: Make an HTTP GET request to the mock API endpoint https://jsonplaceholder.typicode.com/users.
  2. Handle Success: If the request is successful (status code 200-299), the service should return the fetched data.
  3. Handle Network Errors: If a network error occurs (e.g., no internet connection), the service should display an appropriate error message to the user, such as "Network error. Please check your connection."
  4. Handle Server Errors: If the server returns an error (status code 500), the service should display a user-friendly error message, such as "Server error. Please try again later."
  5. Handle Other Errors: For any other error codes, display a generic error message like "An unexpected error occurred."
  6. Use Observables: The service should return an Observable<any> to handle asynchronous operations and error propagation.
  7. Provide Error Messages: The error messages should be displayed to the user in a clear and understandable way. (While the challenge doesn't require a UI component, assume the error messages will be displayed in one).

Examples

Example 1:

Input: Successful API response with user data.
Output: Observable emitting the user data.
Explanation: The service successfully fetches and returns the data.

Example 2:

Input: Network error (e.g., no internet connection).
Output: Observable emitting an error with the message "Network error. Please check your connection."
Explanation: The service detects the network error and returns a relevant error message.

Example 3:

Input: API returns a 500 Internal Server Error.
Output: Observable emitting an error with the message "Server error. Please try again later."
Explanation: The service handles the server error and provides a user-friendly message.

Constraints

  • The service must use Angular's HttpClient for making HTTP requests.
  • The service must return an Observable<any>.
  • Error messages must be informative and user-friendly.
  • The mock API endpoint https://jsonplaceholder.typicode.com/users is considered reliable for testing successful scenarios. You should simulate the 500 error for testing purposes.
  • The service should not throw exceptions directly; it should propagate errors through the Observable.

Notes

  • Consider using the catchError operator in RxJS to handle errors within the Observable stream.
  • You can simulate a 500 error by manually throwing an error within the catchError block or by using a mock HTTP backend for testing.
  • Think about how to provide consistent error handling across your application.
  • Focus on the error handling logic; the exact UI display of the error messages is not required for this challenge. Assume a component will subscribe to the observable and display the error.
Loading editor...
typescript