✅ Add unit testing
This commit is contained in:
@@ -11,6 +11,7 @@ public enum NetworkError: Error {
|
||||
case serverError(String)
|
||||
case decodingError
|
||||
case invalidResponse
|
||||
case notFound
|
||||
}
|
||||
|
||||
extension NetworkError: LocalizedError {
|
||||
@@ -19,18 +20,19 @@ extension NetworkError: LocalizedError {
|
||||
case .badRequest:
|
||||
return NSLocalizedString("Unable to perform request", comment: "badRequestError")
|
||||
case .serverError(let errorMessage):
|
||||
print(errorMessage)
|
||||
return NSLocalizedString(errorMessage, comment: "serverError")
|
||||
case .decodingError:
|
||||
return NSLocalizedString("Unable to decode successfully", comment: "decodingError")
|
||||
case .invalidResponse:
|
||||
return NSLocalizedString("Invalid response", comment: "invalidResponse")
|
||||
case .notFound:
|
||||
return NSLocalizedString("Not Found", comment: "notFound")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enum HTTPMethod {
|
||||
case get([URLQueryItem])
|
||||
case get([URLQueryItem]? = nil)
|
||||
case post(Data?)
|
||||
case put(Data?)
|
||||
case patch(Data?)
|
||||
@@ -110,14 +112,25 @@ public struct Hermes {
|
||||
|
||||
let (data, response) = try await session.data(for: request)
|
||||
|
||||
guard let _ = response as? HTTPURLResponse else {
|
||||
guard let response = response as? HTTPURLResponse else {
|
||||
throw NetworkError.invalidResponse
|
||||
}
|
||||
|
||||
if response.statusCode != 200 {
|
||||
switch response.statusCode {
|
||||
case 404:
|
||||
throw NetworkError.notFound
|
||||
case 500:
|
||||
throw NetworkError.serverError(response.description)
|
||||
default:
|
||||
throw NetworkError.invalidResponse
|
||||
}
|
||||
}
|
||||
|
||||
guard let result = try? JSONDecoder().decode(resource.modelType, from: data) else {
|
||||
throw NetworkError.decodingError
|
||||
}
|
||||
|
||||
|
||||
return result
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user