Skip to content

Commit 173a5d3

Browse files
committed
test: verify error handling in listener calls and update assertions
1 parent 98cc154 commit 173a5d3

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

Sources/SwiftAPIClient/Modifiers/LoggingModifier.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ public extension APIClient.Configs {
135135
if reportMetrics {
136136
updateHTTPMetrics(for: request, status: response?.status, duration: duration, successful: false)
137137
}
138+
let finalError: Error
138139
do {
139140
try errorHandler(
140141
error,
@@ -146,11 +147,12 @@ public extension APIClient.Configs {
146147
fileIDLine: fileIDLine ?? FileIDLine()
147148
)
148149
)
149-
return error
150+
finalError = error
150151
} catch {
151-
listener.onError(id: uuid, error: error, configs: self)
152-
return error
152+
finalError = error
153153
}
154+
listener.onError(id: uuid, error: finalError, configs: self)
155+
return finalError
154156
}
155157

156158
func logRequestCompleted<T>(

Tests/SwiftAPIClientTests/Modifiers/LoggingAndListenerTests.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ final class LoggingAndListenerTests: XCTestCase {
199199
// Verify listener calls
200200
XCTAssertEqual(mockListener.requestStartedCalls.count, 1, "Expected 1 request started call")
201201
XCTAssertEqual(mockListener.responseSerializedCalls.count, 0, "Expected no response serialized calls")
202+
XCTAssertEqual(mockListener.errorCalls.count, 1, "Expected 1 error call")
202203

203204
// In mock caller scenario, error might be handled differently
204205
// The important thing is that request started was called
@@ -393,9 +394,10 @@ final class LoggingAndListenerTests: XCTestCase {
393394
XCTFail("Expected timeout error to be thrown")
394395
} catch {
395396
// Verify HTTP error listener calls
396-
XCTAssertGreaterThan(mockListener.requestStartedCalls.count, 0, "Expected at least 1 request started call")
397+
XCTAssertEqual(mockListener.requestStartedCalls.count, 1, "Expected 1 request started call")
397398
XCTAssertEqual(mockListener.responseReceivedCalls.count, 0, "Expected no response received calls")
398399
XCTAssertEqual(mockListener.responseSerializedCalls.count, 0, "Expected no response serialized calls")
400+
XCTAssertEqual(mockListener.errorCalls.count, 1, "Expected 1 error call")
399401

400402
// Error handling might vary - check if error call was made
401403
if mockListener.errorCalls.count > 0 {

0 commit comments

Comments
 (0)