Overview
The Happy Habitat frontend uses Angular’sHttpClient for all API communications. The application implements a centralized HTTP client architecture with a chain of interceptors for authentication, logging, and error handling.
HTTP Client Configuration
Setup
The HTTP client is configured inapp.config.ts with functional interceptors:
API Base URL
The API base URL is configured in environment files:Interceptors
Execution Order
Interceptors execute in the following order:- Logging Interceptor - Logs outgoing requests and incoming responses
- Error Interceptor - Handles HTTP errors and displays notifications
- Auth Interceptor - Adds authentication tokens to requests
Logging Interceptor
Location:app/interceptors/logging.interceptor.ts:1
Logs all HTTP requests and responses with detailed metadata:
Features:
- Request logging with method, URL, and sanitized headers
- Response logging with status, duration, and size
- Error logging with full error details
- Slow request detection (warns if duration > 5 seconds)
- Sensitive header redaction (Authorization, Cookie, etc.)
Error Interceptor
Location:app/interceptors/error.interceptor.ts:1
Centralizes HTTP error handling and user notifications:
Status Code Handling:
| Status Code | Type | Action |
|---|---|---|
| 400 | Validation | Show notification with validation errors |
| 401 | Unauthorized | Redirect to login (if not already there) |
| 403 | Forbidden | Show permission denied notification |
| 404 | Not Found | Log silently (no notification) |
| 422 | Validation | Show validation error notification |
| 429 | Rate Limit | Show rate limiting notification |
| 500+ | Server Error | Show server error notification |
Auth Interceptor
Location:app/interceptors/auth.interceptor.ts:1
Manages authentication tokens and automatic token refresh:
Features:
- Automatic token injection into request headers
- Automatic token refresh on 401 responses
- Request retry after token refresh
- Graceful logout on refresh failure
Service Architecture
Base Service Pattern
All API services follow a consistent pattern:Common Patterns
Query Parameters
Request Body
Response Transformation
Caching Strategy
Some services implement local caching to reduce API calls:Cache Implementation
Cache Invalidation
Error Handling
Service-Level Error Handling
Each service handles errors in a standard way:Error Service Integration
The ErrorService normalizes and handles errors:- Normalizes different error types (HTTP, JavaScript, Generic)
- Logs errors with appropriate severity
- Shows user-friendly notifications
- Tracks active errors
- Supports custom error handlers
src/app/services/error.service.ts and handles global error logging and display.