Skip to content

Commit 582142c

Browse files
committed
Implemented error handling for each request.
- implemented error handling for all the API requests. - In dev and staging environments, if the Json parser throw's exception it will show the Json data, where as in prod it will show error message as "Not able to process the response at this moment. Please try again later." Ticket: https://dev.azure.com/TDEI-UW/TDEI/_workitems/edit/2035
1 parent e1ad7d4 commit 582142c

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

GoInfoGame/GoInfoGame/Network/ApiManager.swift

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,12 @@ class ApiManager {
113113
return
114114
}
115115

116-
if let httpResponse = response as? HTTPURLResponse,
117-
httpResponse.statusCode == 401,
116+
guard let httpResponse = response as? HTTPURLResponse else {
117+
completion(.failure(APIError.custom("Invalid response, Please try again later.")))
118+
return
119+
}
120+
121+
if httpResponse.statusCode == 401,
118122
request.url!.lastPathComponent.contains("refresh-token") == false,
119123
request.url!.lastPathComponent.contains("authenticate") == false {
120124
print("Failed requests: \(String(describing: request.url))")
@@ -142,6 +146,17 @@ class ApiManager {
142146
return
143147
}
144148

149+
switch httpResponse.statusCode {
150+
case 400...499:
151+
completion(.failure(APIError.custom("Client Error. Please try again later.")))
152+
return
153+
case 500...599:
154+
completion(.failure(APIError.custom("Internal server error. Please try again later.")))
155+
return
156+
default:
157+
print("Request processed successfully.")
158+
}
159+
145160
guard let data = data else {
146161
completion(.failure(APIError.custom("No data returned")))
147162
return
@@ -167,11 +182,15 @@ class ApiManager {
167182
} catch {
168183
print("Failed to decode data: \(error.localizedDescription)")
169184
if let dataString = String(data: data, encoding: .utf8), !dataString.isEmpty {
170-
completion(.failure(APIError.custom("Not a valid JSON \(dataString)")))
185+
if APIConfiguration.shared.environment == .development ||
186+
APIConfiguration.shared.environment == .staging {
187+
completion(.failure(APIError.custom("Not a valid JSON \(dataString)")))
188+
} else {
189+
completion(.failure(APIError.custom("Not able to process the response at this moment. Please try again later.")))
190+
}
171191
} else {
172192
completion(.failure(APIError.custom("Empty JSON")))
173193
}
174-
175194
return
176195
}
177196
}

0 commit comments

Comments
 (0)