File tree Expand file tree Collapse file tree
SwiftUIExamples/SwiftUIExamples/AsyncAwait Expand file tree Collapse file tree Original file line number Diff line number Diff 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 {
You can’t perform that action at this time.
0 commit comments