Skip to content

Commit

Permalink
feat: improve error handling for network errors
Browse files Browse the repository at this point in the history
  • Loading branch information
julien-ctx committed Nov 21, 2023
1 parent f003607 commit 62be740
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
3 changes: 1 addition & 2 deletions SwiftyCompanion/Network/Models/User.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ struct User: Codable {

// MARK: - Image
struct APIImage: Codable {
let link: String
let versions: Versions

struct Versions: Codable {
let large, medium, small, micro: String
let large, medium, small, micro: String?
}
}

Expand Down
34 changes: 28 additions & 6 deletions SwiftyCompanion/Network/NetworkContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,19 @@ class NetworkContext {

let (data, response) = try await URLSession.shared.data(for: request)

guard let httpResponse = response as? HTTPURLResponse,
(200...299).contains(httpResponse.statusCode) else {
throw NetworkError.noData
guard let httpResponse = response as? HTTPURLResponse else {
throw NetworkError.invalidResponse
}

if !(200...299).contains(httpResponse.statusCode) {
throw NetworkError.httpError(statusCode: httpResponse.statusCode)
}

var decodedResponse: User?
do {
decodedResponse = try JSONDecoder().decode(User.self, from: data)
} catch {
print("Error decoding JSON: \(error)")
throw NetworkError.decodingError
}
return decodedResponse
}
Expand All @@ -123,6 +127,24 @@ class NetworkContext {

enum NetworkError: Error {
case invalidURL
case noData
case decodingFailed
case invalidResponse
case decodingError
case httpError(statusCode: Int)

var errorMessage: String {
switch self {
case .invalidURL:
return "The URL could not be reached."
case .invalidResponse:
return "No data received from the server."
case .decodingError:
return "User data couln't be decoded correctly."
case .httpError(let statusCode):
if statusCode == 404 {
return "User not found."
} else {
return "HTTP request failed with status code: \(statusCode)."
}
}
}
}
20 changes: 16 additions & 4 deletions SwiftyCompanion/Views/UserDetailsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@ struct UserDetailsView: View {
@State var isLoading: Bool = true
@State var user: User?
@State var currentView: ViewType = .projects
@State var errorMessage: String?

let login: String

var body: some View {
Group {
if isLoading {
if let errorMessage = errorMessage {
VStack {
Text("Error".uppercased())
.font(.system(size: 28, weight: .bold, design: .default))
Text(errorMessage)
}
} else if isLoading {
ProgressView()
.progressViewStyle(CircularProgressViewStyle())
.scaleEffect(2)
Expand All @@ -29,7 +36,9 @@ struct UserDetailsView: View {
.frame(minWidth: 0, maxWidth: .infinity)
ScrollView(.vertical) {
VStack(spacing: 7) {
UserImage(userImage: user.image.versions.medium)
if let image = user.image.versions.medium {
UserImage(userImage: image)
}
MainInformation(firstName: user.firstName, lastName: user.lastName, login: login)
SecondaryInformation(user: user)
if user.staff != true {
Expand Down Expand Up @@ -100,11 +109,14 @@ struct UserDetailsView: View {
user = info
isLoading = false
} else {
print("No user info")
errorMessage = "No user information."
}
} catch let error as NetworkError {
errorMessage = "\(error.errorMessage)"
} catch {
print("Error in loadUserInformation")
errorMessage = "Something went wrong."
}

}

private func hasMatchingProjects() -> Bool {
Expand Down

0 comments on commit 62be740

Please sign in to comment.