ScreenPulse uses a centralized error handling approach with Angular’s HTTP interceptors and ngx-toastr for user-friendly error notifications. This guide covers the error handling patterns used throughout the application.
The ErrorInterceptor is the core component for handling HTTP errors globally. It intercepts all HTTP responses and transforms errors into user-friendly messages.
import { Injectable } from '@angular/core';import { HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpErrorResponse} from '@angular/common/http';import { Observable, throwError } from 'rxjs';import { catchError } from 'rxjs/operators';@Injectable()export class ErrorInterceptor implements HttpInterceptor { intercept(req: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> { return next.handle(req).pipe( catchError((error: HttpErrorResponse) => { let message = 'An unknown error occurred.'; switch (error.status) { case 0: message = 'Cannot connect to server. Please check your internet connection'; break; case 401: message = 'Unauthorized access. Please try it again'; if (error.error.code === 'AUTH_TOKEN_EXPIRED') { message = 'Session expired. Please login again'; } break; case 404: message = 'The favorite you are trying to delete could not be found in your list. Please try again later.'; break; case 409: if (error.error.code === 'USER_EXISTS') { message = 'User with this email already exists'; } if (error.error.code === 'FAVORITE_EXISTS') { message = 'Media item already in favorites'; } break; default: message = 'An unexpected error occurred. Please try again later'; break; } return throwError(() => ({ message, status: error.status, })); }) ); }}
The interceptor handles different HTTP status codes:
Status Code 0 - Network Error
Scenario: Server is unreachable or network connectivity issuesMessage: “Cannot connect to server. Please check your internet connection”Use Case: When the user is offline or the backend server is down
Status Code 401 - Unauthorized
Scenario: Authentication required or token expiredMessages:
Default: “Unauthorized access. Please try it again”
Use Case: When accessing protected routes without valid authentication
Status Code 404 - Not Found
Scenario: Resource not foundMessage: “The favorite you are trying to delete could not be found in your list. Please try again later.”Use Case: Attempting to delete a favorite that doesn’t exist
Status Code 409 - Conflict
Scenario: Resource conflictMessages:
USER_EXISTS: “User with this email already exists”
FAVORITE_EXISTS: “Media item already in favorites”
Use Case: Duplicate user registration or adding duplicate favorites
The ErrorInterceptor automatically catches all HTTP errors, so individual services don’t need to implement catchError unless they need custom error handling logic.
Keep all error messages in the interceptor for consistency:
// ✅ Good - Centralized in interceptorcase 409: if (error.error.code === 'FAVORITE_EXISTS') { message = 'Media item already in favorites'; } break;// ❌ Bad - Scattered across componentsthis.toastr.error('Media item already in favorites');