@@ -10,7 +10,7 @@ import Foundation
1010/// Base networking service that handles HTTP requests and authentication
1111class NetworkService : ObservableObject {
1212 // MARK: - Configuration
13- @Published var baseURL : String = " "
13+ @Published var baseURL : String = " http://localhost:2283 "
1414 @Published var accessToken : String ?
1515
1616 private let session = URLSession . shared
@@ -114,21 +114,42 @@ class NetworkService: ObservableObject {
114114 }
115115 }
116116
117- let ( data, response) = try await session. data ( for: request)
117+ let ( data, response) : ( Data , URLResponse )
118+ do {
119+ ( data, response) = try await session. data ( for: request)
120+ } catch {
121+ print ( " NetworkService: Network error occurred: \( error) " )
122+ // Handle network connectivity issues (timeouts, connection refused, DNS failures, etc.)
123+ throw ImmichError . networkError
124+ }
118125
119126 guard let httpResponse = response as? HTTPURLResponse else {
120127 print ( " NetworkService: Invalid HTTP response " )
121- throw ImmichError . serverError
128+ throw ImmichError . networkError
122129 }
123130
124131 print ( " NetworkService: Response status code: \( httpResponse. statusCode) " )
125132
126133 guard httpResponse. statusCode == 200 else {
127- print ( " NetworkService: Server error with status \( httpResponse. statusCode) " )
134+ print ( " NetworkService: HTTP error with status \( httpResponse. statusCode) " )
128135 if let responseString = String ( data: data, encoding: . utf8) {
129136 print ( " NetworkService: Response body: \( responseString) " )
130137 }
131- throw ImmichError . serverError
138+
139+ // Classify HTTP status codes into appropriate ImmichError types
140+ switch httpResponse. statusCode {
141+ case 401 :
142+ throw ImmichError . notAuthenticated
143+ case 403 :
144+ throw ImmichError . forbidden
145+ case 500 ... 599 :
146+ throw ImmichError . serverError ( httpResponse. statusCode)
147+ case 400 ... 499 :
148+ throw ImmichError . clientError ( httpResponse. statusCode)
149+ default :
150+ // For any other status codes, treat as server error
151+ throw ImmichError . serverError ( httpResponse. statusCode)
152+ }
132153 }
133154
134155 do {
@@ -157,11 +178,37 @@ class NetworkService: ObservableObject {
157178 var request = URLRequest ( url: url)
158179 request. setValue ( " Bearer \( accessToken) " , forHTTPHeaderField: " Authorization " )
159180
160- let ( data, response) = try await session. data ( for: request)
181+ let ( data, response) : ( Data , URLResponse )
182+ do {
183+ ( data, response) = try await session. data ( for: request)
184+ } catch {
185+ print ( " NetworkService: Network error occurred in makeDataRequest: \( error) " )
186+ // Handle network connectivity issues (timeouts, connection refused, DNS failures, etc.)
187+ throw ImmichError . networkError
188+ }
189+
190+ guard let httpResponse = response as? HTTPURLResponse else {
191+ print ( " NetworkService: Invalid HTTP response in makeDataRequest " )
192+ throw ImmichError . networkError
193+ }
161194
162- guard let httpResponse = response as? HTTPURLResponse ,
163- httpResponse. statusCode == 200 else {
164- throw ImmichError . serverError
195+ guard httpResponse. statusCode == 200 else {
196+ print ( " NetworkService: HTTP error in makeDataRequest with status \( httpResponse. statusCode) " )
197+
198+ // Classify HTTP status codes into appropriate ImmichError types
199+ switch httpResponse. statusCode {
200+ case 401 :
201+ throw ImmichError . notAuthenticated
202+ case 403 :
203+ throw ImmichError . forbidden
204+ case 500 ... 599 :
205+ throw ImmichError . serverError ( httpResponse. statusCode)
206+ case 400 ... 499 :
207+ throw ImmichError . clientError ( httpResponse. statusCode)
208+ default :
209+ // For any other status codes, treat as server error
210+ throw ImmichError . serverError ( httpResponse. statusCode)
211+ }
165212 }
166213
167214 return data
@@ -178,21 +225,36 @@ enum HTTPMethod: String {
178225}
179226
180227enum ImmichError : Error , LocalizedError {
181- case notAuthenticated
182- case invalidURL
183- case serverError
184- case networkError
228+ case notAuthenticated // 401 - Invalid/expired token
229+ case forbidden // 403 - Access denied
230+ case invalidURL // Malformed URL
231+ case serverError( Int ) // 5xx - Server issues
232+ case networkError // Network connectivity issues
233+ case clientError( Int ) // 4xx (except 401/403)
234+
235+ var shouldLogout : Bool {
236+ switch self {
237+ case . notAuthenticated, . forbidden:
238+ return true
239+ case . serverError, . networkError, . invalidURL, . clientError:
240+ return false
241+ }
242+ }
185243
186244 var errorDescription : String ? {
187245 switch self {
188246 case . notAuthenticated:
189247 return " Not authenticated. Please log in. "
248+ case . forbidden:
249+ return " Access forbidden. Please check your permissions. "
190250 case . invalidURL:
191251 return " Invalid URL "
192- case . serverError:
193- return " Server error occurred "
252+ case . serverError( let statusCode ) :
253+ return " Server error occurred (HTTP \( statusCode ) ) "
194254 case . networkError:
195255 return " Network error occurred "
256+ case . clientError( let statusCode) :
257+ return " Client error occurred (HTTP \( statusCode) ) "
196258 }
197259 }
198- }
260+ }
0 commit comments