Skip to content

Commit

Permalink
Improve error type definitions for more clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
okhan-okbay-cko committed Mar 19, 2024
1 parent bb96091 commit 280e7e9
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
6 changes: 3 additions & 3 deletions Sources/CheckoutNetwork/CheckoutClientInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import Foundation
public protocol CheckoutClientInterface {

/// Completion handler that will return a result containing a decodable object or an error
typealias CompletionHandler<T> = ((Result<T, Error>) -> Void)
typealias CompletionHandler<T> = ((Result<T, CheckoutNetworkError>) -> Void)

/// Completion handler that will return errors if it fails or nothing if it completes as expected
typealias NoDataResponseCompletionHandler = ((Error?) -> Void)
typealias NoDataResponseCompletionHandler = ((CheckoutNetworkError?) -> Void)

// MARK: Traditional Base Methods

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public extension CheckoutNetworkClient {

func runRequest<T: Decodable>(with configuration: RequestConfiguration) async throws -> T {
return try await withCheckedThrowingContinuation { continuation in
runRequest(with: configuration) { (result: Result<T, Error>) in
runRequest(with: configuration) { (result: Result<T, CheckoutNetworkError>) in
switch result {
case .success(let response):
continuation.resume(returning: response)
Expand All @@ -24,7 +24,7 @@ public extension CheckoutNetworkClient {

func runRequest(with configuration: RequestConfiguration) async throws {
return try await withCheckedThrowingContinuation { continuation in
runRequest(with: configuration) { (error: Error?) in
runRequest(with: configuration) { (error: CheckoutNetworkError?) in

guard let error = error else {
continuation.resume(returning: Void())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation

extension CheckoutNetworkClient {
func getErrorFromResponse(_ response: URLResponse?, data: Data?) -> Error? {
func getErrorFromResponse(_ response: URLResponse?, data: Data?) -> CheckoutNetworkError? {
guard let response = response as? HTTPURLResponse else {
return CheckoutNetworkError.invalidURLResponse
}
Expand Down
9 changes: 5 additions & 4 deletions Sources/CheckoutNetwork/CheckoutNetworkClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ public class CheckoutNetworkClient: CheckoutClientInterface {
self?.tasks.removeValue(forKey: taskID)
guard let self = self else { return }
if let error = error {
completionHandler(.failure(self.convertDataTaskErrorsToCheckoutNetworkError(error: error)))
completionHandler(.failure(self.convertDataTaskErrorsToCheckoutNetworkError(error: error)))
return
}

guard let data = data else {
completionHandler(.failure(CheckoutNetworkError.noDataResponseReceived))
completionHandler(.failure(.noDataResponseReceived))
return
}

Expand All @@ -50,7 +50,7 @@ public class CheckoutNetworkClient: CheckoutClientInterface {
let dataResponse = try JSONDecoder().decode(T.self, from: data)
completionHandler(.success(dataResponse))
} catch {
completionHandler(.failure(error))
completionHandler(.failure(.decoding(errorDescription: error.localizedDescription)))
}
}
self.tasks[taskID] = task
Expand All @@ -66,8 +66,9 @@ public class CheckoutNetworkClient: CheckoutClientInterface {
let task = session.dataTask(with: request) { [weak self] data, response, error in
self?.tasks.removeValue(forKey: taskID)
guard let self = self else { return }

if let error = error {
completionHandler(error)
completionHandler(self.convertDataTaskErrorsToCheckoutNetworkError(error: error))
return
}
if let responseError = self.getErrorFromResponse(response, data: nil) {
Expand Down
10 changes: 9 additions & 1 deletion Sources/CheckoutNetwork/CheckoutNetworkError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ public enum CheckoutNetworkError: LocalizedError, Equatable {
/// When a response could not be bound to an instance of HTTPURLResponse
case invalidURLResponse

/// A decoding error has been received while decoding the API response
case decoding(errorDescription: String)

/// Network response was not in the 200 range
case unexpectedHTTPResponse(code: Int)

/// Network call and completion appear valid but no data was returned making the parsing impossible. Use runRequest method with NoDataResponseCompletionHandler if no data is expected (HTTP 204 is a success case with no content being returned)
/// Network call and completion appear valid but no data was returned making the parsing impossible.
/// Use runRequest method with NoDataResponseCompletionHandler if no data is expected
/// Refresher: HTTP 204 is a success case with no content being returned
case noDataResponseReceived

/// Network response returned with HTTP Code 422
Expand Down Expand Up @@ -48,6 +53,9 @@ public enum CheckoutNetworkError: LocalizedError, Equatable {
case .invalidURLResponse:
return "Could not instantiate an HTTPURLResponse with the received response value"

case .decoding(let errorDescription):
return errorDescription

case .unexpectedHTTPResponse(let code):
return "Received an unexpected HTTP response: \(code)"

Expand Down

0 comments on commit 280e7e9

Please sign in to comment.