Network Error Handling
Relevant source files
The following files were used as context for generating this wiki page:
The Network Error Handling system in Social-Connect provides a structured approach to managing failures during data transmission between the mobile client and the backend API. It encompasses URL validation, authentication token verification, transport-level error detection, and HTTP status code interpretation.
The system ensures that network requests initiated by services like UserService are processed through a central APIClient, which standardizes how errors are caught and propagated back to the calling site via completion handlers.
Architecture and Components
The network error handling logic is primarily encapsulated within the APIClient class, which serves as the gateway for all HTTP communications. It interacts with the KeychainManager to retrieve security credentials and provides feedback to service layers.
Key Components
| Component | Responsibility |
|---|---|
APIClient | Validates URLs, constructs requests, and interprets URLSession results. |
UserService | Consumes network results and triggers UI/Data notifications based on success/failure. |
KeychainManager | Provides the necessary authentication tokens for request headers. |
URLSession | The underlying Apple framework used to execute data tasks and return raw errors. |
Sources: APIClient.swift:3-5, UserService.swift:4-6, KeychainManager.swift:6-8
Request Lifecycle and Error Detection
The network request flow involves multiple stages where errors can occur, from initial URL construction to the final evaluation of the HTTP status code.
1. URL Validation
Before a request is dispatched, the APIClient validates the endpoint string combined with the baseURL. If the URL is malformed, an NSError with the domain "APIClient" and code 0 is returned immediately.
Sources: APIClient.swift:8-11
2. Authentication Integrity
The client attempts to inject a Bearer token into the Authorization header. While the current implementation does not explicitly throw an error if the token is missing, the lack of a token may lead to downstream 401 Unauthorized errors from the server.
Sources: APIClient.swift:18-20, KeychainManager.swift:10-25
3. Transport and Protocol Errors
The system utilizes URLSession.shared.dataTask to handle the execution of requests. This layer catches transport-level issues such as:
- Loss of connectivity
- DNS resolution failures
- Timeout issues
If an error is present in the dataTask completion block, the client returns false and the specific Error object to the caller.
Sources: APIClient.swift:23-27
4. HTTP Response Validation
The system verifies that the response is a valid HTTPURLResponse. If the response is not an HTTP response, it generates a custom error indicating an "Invalid response".
The final success state is determined by checking if the status code falls within the successful range (200-299).
Sources: APIClient.swift:29-34
Data Flow Diagram
The following diagram illustrates the error-checking gates within a standard POST request:
This diagram shows the sequential validation steps performed in APIClient.post.
Sources: APIClient.swift:7-35
Implementation in Services
Services such as UserService leverage the error handling provided by APIClient. For instance, when following or unfollowing a user, the service checks the success boolean. It only triggers local notifications (e.g., userFollowStatusChanged) if the network operation was successful.
apiClient.post(endpoint: endpoint) { success, error in
// Update local user cache if successful
if success {
NotificationCenter.default.post(name: .userFollowStatusChanged, object: nil)
}
completion?(success, error)
}
Sources: UserService.swift:9-17, UserService.swift:21-26
Integration with App Lifecycle
Errors can also originate from external triggers like Deep Links. The AppDelegate routes URLs through the DeepLinkHandler, which interacts with the UserService. If a deep link contains invalid parameters (e.g., a missing userId), the system returns false, preventing further network calls.
This sequence shows how deep link processing interfaces with the network service and its error handling.
Sources: AppDelegate.swift:15-18, DeepLinkHandler.swift:7-30, UserService.swift:9-12
Summary
Network error handling in Social-Connect is centralized in the APIClient, which provides a consistent interface for managing URL errors, transport failures, and HTTP status codes. By decoupling the error detection logic from the business services, the architecture ensures that components like UserService can react predictably to network outcomes while maintaining data integrity through conditional notifications.