Skip to content

Commit 4877bc7

Browse files
Managed status codes
1 parent ac46810 commit 4877bc7

1 file changed

Lines changed: 15 additions & 1 deletion

File tree

SwiftUIExamples/SwiftUIExamples/AsyncAwait/git_hub_repos_async_app.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,22 @@ class GitHubService: GitHubServiceProtocol {
4848
}
4949

5050
do {
51-
let (data, _) = try await URLSession.shared.data(from: url)
51+
let (data, response) = try await URLSession.shared.data(from: url)
52+
53+
// ✅ Check for valid HTTP response
54+
guard let httpResponse = response as? HTTPURLResponse else {
55+
throw GitHubServiceError.networkError(URLError(.badServerResponse))
56+
}
57+
58+
// ✅ Ensure status code is 200
59+
guard (200...299).contains(httpResponse.statusCode) else {
60+
throw GitHubServiceError.networkError(
61+
URLError(.init(rawValue: httpResponse.statusCode))
62+
)
63+
}
64+
5265
return try JSONDecoder().decode([GitHubRepo].self, from: data)
66+
5367
} catch let decodingError as DecodingError {
5468
throw GitHubServiceError.decodingError(decodingError)
5569
} catch {

0 commit comments

Comments
 (0)