Skip to content

Commit db9a62e

Browse files
KAPE-1648 Add extra logs
1 parent 650490c commit db9a62e

2 files changed

Lines changed: 30 additions & 10 deletions

File tree

LocalPackages/PIALibrary/Sources/PIALibrary/Account/Data/ClientErrorMapper.swift

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
2-
31
import Foundation
42

3+
private let log = PIALogger.logger(for: ClientErrorMapper.self)
4+
55
/// Maps an Network Error with a ClientError
66
/// The idea is to use this mapper on `PIAWebServices` to map the errors returned from the Swift implementation of the Accounts Lib with the ones that the app expects
77
struct ClientErrorMapper {
88
static func map(networkRequestError: NetworkRequestError) -> ClientError {
9+
log.info("Will map ClientError from NetworkRequestError: \(networkRequestError)")
10+
911
switch networkRequestError {
1012
case .connectionError(let statusCode, let message):
1113
return getClientError(from: statusCode) ?? .unexpectedReply
@@ -49,17 +51,25 @@ struct ClientErrorMapper {
4951
}
5052

5153
static func getClientError(from statusCode: Int?) -> ClientError? {
52-
53-
guard let statusCode,
54-
let httpStatusCode = HttpResponseStatusCode(rawValue: statusCode) else {
54+
guard
55+
let statusCode,
56+
let httpStatusCode = HttpResponseStatusCode(rawValue: statusCode)
57+
else {
58+
log.error("Unable to map ClientError due to lack of statusCode")
5559
return nil
5660
}
61+
5762
switch httpStatusCode {
5863
case .unauthorized:
64+
log.info("Did map \(ClientError.unauthorized) out of httpStatusCode: \(httpStatusCode)")
5965
return .unauthorized
66+
6067
case .throttled:
68+
log.info("Did map \(ClientError.throttled(retryAfter: 60)) out of httpStatusCode: \(httpStatusCode)")
6169
return .throttled(retryAfter: 60)
70+
6271
default:
72+
log.info("Mapped no ClientError out of httpStatusCode: \(httpStatusCode)")
6373
return nil
6474
}
6575
}

LocalPackages/PIALibrary/Sources/PIALibrary/Common/Data/Networking/NetworkRequestClient.swift

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import Foundation
33
import NWHttpConnection
44

5+
private let log = PIALogger.logger(for: NetworkRequestClient.self)
56

67
protocol NetworkRequestClientType {
78
typealias Completion = ((NetworkRequestError?, NetworkRequestResponseType?) -> Void)
@@ -42,8 +43,9 @@ private extension NetworkRequestClient {
4243

4344
/// Serial execution of all the connections until one succeeds or completes with an error when all connection attempts fail
4445
func executeRecursivelyUntilSuccess(connections: [NWHttpConnectionType], completion: @escaping NetworkRequestClientType.Completion) {
45-
46+
4647
guard !connections.isEmpty else {
48+
log.error("All connection attempts failed: No connections available")
4749
completion(.allConnectionAttemptsFailed(statusCode: nil), nil)
4850
return
4951
}
@@ -54,6 +56,7 @@ private extension NetworkRequestClient {
5456
func tryNextConnectionOrFail(currentStatusCode: Int?) {
5557
if remainingConnections.isEmpty {
5658
// No more endpoints to try a connection
59+
log.error("All connection attempts failed: No more endpoints to try (statusCode: \(currentStatusCode?.description ?? "nil"))")
5760
let requestError = NetworkRequestError.allConnectionAttemptsFailed(statusCode: currentStatusCode)
5861
completion(requestError, nil)
5962
} else {
@@ -64,20 +67,23 @@ private extension NetworkRequestClient {
6467

6568
execute(connection: nextConnection) { error, responseData in
6669

67-
if error != nil {
70+
if let error = error {
71+
log.error("Connection error: \(error) (statusCode: \(responseData?.statusCode?.description ?? "nil"))")
6872
tryNextConnectionOrFail(currentStatusCode: responseData?.statusCode)
6973
} else if let responseData {
7074
let statusCode: Int = responseData.statusCode ?? -1
7175
let isSuccessStatusCode = statusCode > 199 && statusCode < 300
72-
76+
7377
if isSuccessStatusCode {
7478
completion(nil, responseData)
7579
} else {
7680
// Connection did not succeed, try the next one
81+
log.error("Non-success status code received: \(statusCode)")
7782
tryNextConnectionOrFail(currentStatusCode: responseData.statusCode)
7883
}
7984
} else {
8085
// No error and no data
86+
log.error("Connection completed with no error and no data")
8187
tryNextConnectionOrFail(currentStatusCode: nil)
8288
}
8389
}
@@ -88,25 +94,29 @@ private extension NetworkRequestClient {
8894
func execute(connection: NWHttpConnectionType, completion: @escaping NetworkRequestClientType.Completion) {
8995
do {
9096
var connectionHandled: Bool = false
91-
97+
9298
try connection.connect { error, dataResponse in
9399
if let error {
94100
connectionHandled = true
101+
log.error("Connection error in single connection: \(error.localizedDescription) (statusCode: \(dataResponse?.statusCode?.description ?? "nil"))")
95102
completion(NetworkRequestError.connectionError(statusCode: dataResponse?.statusCode, message: error.localizedDescription), nil)
96103
} else if let dataResponse = dataResponse as? NetworkRequestResponseType {
97104
connectionHandled = true
98105
completion(nil, dataResponse)
99106
} else {
100107
connectionHandled = true
108+
log.error("No error and no response in single connection")
101109
completion(NetworkRequestError.noErrorAndNoResponse, nil)
102110
}
103111
} completion: {
104112
if connectionHandled == false {
113+
log.error("Connection completed with no response")
105114
completion(NetworkRequestError.connectionCompletedWithNoResponse, nil)
106115
}
107116
}
108-
117+
109118
} catch {
119+
log.error("Unknown error in connection execution: \(error.localizedDescription)")
110120
completion(NetworkRequestError.unknown(message: error.localizedDescription), nil)
111121
}
112122
}

0 commit comments

Comments
 (0)