tangled
alpha
login
or
join now
sparrowtek.com
/
CoreATProtocol
2
fork
atom
this repo has no description
2
fork
atom
overview
issues
pulls
pipelines
remove debug prints
radmakr.com
2 weeks ago
fa62835c
fdad33c3
+3
-27
1 changed file
expand all
collapse all
unified
split
Sources
CoreATProtocol
Networking.swift
+3
-27
Sources/CoreATProtocol/Networking.swift
···
126
126
public func didReceiveErrorResponse(_ response: HTTPURLResponse) async {
127
127
let nonce = response.value(forHTTPHeaderField: "DPoP-Nonce")
128
128
?? response.value(forHTTPHeaderField: "dpop-nonce")
129
129
-
debugPrint("[APRouterDelegate] didReceiveErrorResponse: status=\(response.statusCode), DPoP-Nonce=\(nonce ?? "nil")")
130
129
if let nonce {
131
130
await storeDPoPNonce(nonce)
132
131
}
···
138
137
}
139
138
140
139
public func shouldRetry(error: Error, attempts: Int) async throws -> Bool {
141
141
-
guard attempts <= 2 else {
142
142
-
debugPrint("[APRouterDelegate] shouldRetry: attempts=\(attempts), giving up")
143
143
-
return false
144
144
-
}
145
145
-
146
146
-
// Extract error body for logging
147
147
-
let errorBody: String
148
148
-
if let networkError = error as? NetworkError,
149
149
-
case .statusCode(_, let d, _) = networkError {
150
150
-
errorBody = String(data: d, encoding: .utf8) ?? "<non-utf8>"
151
151
-
} else {
152
152
-
errorBody = String(describing: error)
153
153
-
}
154
154
-
debugPrint("[APRouterDelegate] shouldRetry: attempts=\(attempts), body=\(errorBody)")
140
140
+
guard attempts <= 2 else { return false }
155
141
156
142
// DPoP nonce challenge — retry immediately without token refresh.
157
143
// Allowed on any attempt since a nonce challenge can follow a token refresh.
158
158
-
if isDPoPNonceError(from: error) {
159
159
-
debugPrint("[APRouterDelegate] shouldRetry: DPoP nonce challenge, retrying")
160
160
-
return true
161
161
-
}
144
144
+
if isDPoPNonceError(from: error) { return true }
162
145
163
146
// Token expired or unauthorized — attempt OAuth refresh (first attempt only).
164
164
-
guard attempts == 1 else {
165
165
-
debugPrint("[APRouterDelegate] shouldRetry: attempts > 1 and not a nonce error, giving up")
166
166
-
return false
167
167
-
}
147
147
+
guard attempts == 1 else { return false }
168
148
169
149
if case .network(let networkError) = error as? AtError,
170
150
case .statusCode(let statusCode, _, _) = networkError,
171
151
let statusCode = statusCode?.rawValue,
172
152
statusCode == 401 || statusCode == 403 {
173
173
-
debugPrint("[APRouterDelegate] shouldRetry: AtError 401/403, refreshing OAuth")
174
153
return try await refreshViaOAuth()
175
154
}
176
155
177
156
if case .statusCode(let statusCode, _, _) = error as? NetworkError,
178
157
let statusCode = statusCode?.rawValue,
179
158
statusCode == 401 || statusCode == 403 {
180
180
-
debugPrint("[APRouterDelegate] shouldRetry: NetworkError 401/403, refreshing OAuth")
181
159
return try await refreshViaOAuth()
182
160
}
183
161
184
162
if case .message(let message) = error as? AtError,
185
163
message.error == AtErrorType.expiredToken.rawValue {
186
186
-
debugPrint("[APRouterDelegate] shouldRetry: ExpiredToken, refreshing OAuth")
187
164
return try await refreshViaOAuth()
188
165
}
189
166
190
190
-
debugPrint("[APRouterDelegate] shouldRetry: no matching case, not retrying")
191
167
return false
192
168
}
193
169