Skip to content

Commit

Permalink
Merge pull request #7 from MFB-Technologies-Inc/feature/improve-mappi…
Browse files Browse the repository at this point in the history
…ng-of-errors

Feature/improve mapping of errors
  • Loading branch information
roanutil authored Mar 6, 2023
2 parents 71806bf + 8975952 commit 78a253d
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ extension NetworkServiceClient {
}
return result
.httpMap()
.mapToNetworkError()
}

private func response(_ request: URLRequest) async throws -> (Data, URLResponse) {
Expand Down
16 changes: 7 additions & 9 deletions Sources/NetworkServiceAsyncBeta/Result+NetworkService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,7 @@ extension Result where Success == (Data, URLResponse), Failure == Error {
}
return .success(data)
}
.mapError { error in
guard let failure = error as? NetworkService.Failure else {
return .unknown(error as NSError)
}
return failure
}
.mapToNetworkError()
}
}

Expand All @@ -38,10 +33,13 @@ extension Result {
/// - `Publishers.MapError<Self, NetworkService.Failure>`
public func mapToNetworkError() -> Result<Success, NetworkService.Failure> {
mapError { error in
guard let failure = error as? NetworkService.Failure else {
return NetworkService.Failure.unknown(error as NSError)
if let urlError = error as? URLError {
return .urlError(urlError)
} else if let failure = error as? NetworkService.Failure {
return failure
} else {
return .unknown(error as NSError)
}
return failure
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ final class NetworkServiceTestHelper: XCTestCase {
let mock = MockNetworkService(scheduler: scheduler)
mock.outputs = [RepeatResponse.repeatInfinite(MockingBird.chirp)]
for _ in 0 ..< 5 {
let result: Result<MockingBird, NetworkService.Failure> = await mock.get(try url())
let result: Result<MockingBird, NetworkService.Failure> = try await mock.get(url())
XCTAssertEqual(try result.get(), .chirp)
}
let queuedOutput = try XCTUnwrap(mock.outputs.first as? RepeatResponse)
Expand All @@ -58,7 +58,7 @@ final class NetworkServiceTestHelper: XCTestCase {
let mock = MockNetworkService(scheduler: scheduler)
mock.outputs = [RepeatResponse.repeat(MockingBird(chirp: true), count: 5)]
for _ in 0 ..< 5 {
let result: Result<MockingBird, NetworkService.Failure> = await mock.get(try url())
let result: Result<MockingBird, NetworkService.Failure> = try await mock.get(url())
XCTAssertEqual(try result.get(), .chirp)
}
XCTAssert(mock.outputs.isEmpty, "Output queue is empty after the specified number of repititions")
Expand All @@ -69,7 +69,7 @@ final class NetworkServiceTestHelper: XCTestCase {
mock.delay = Delay.seconds(2)
mock.outputs = [MockingBird.chirp]
let startTime = Date()
let result: Result<MockingBird, NetworkService.Failure> = await mock.get(try url())
let result: Result<MockingBird, NetworkService.Failure> = try await mock.get(url())
let endTime = Date()
let duration = startTime.distance(to: endTime)
XCTAssertGreaterThan(duration, 2)
Expand All @@ -84,7 +84,7 @@ final class NetworkServiceTestHelper: XCTestCase {
let expectation = expectation(description: "Never receive a response")
expectation.isInverted = true
let task = Task {
let result: Result<MockingBird, NetworkService.Failure> = await mock.get(try url())
let result: Result<MockingBird, NetworkService.Failure> = try await mock.get(url())
XCTAssertEqual(try result.get(), MockingBird.chirp)
expectation.fulfill()
}
Expand Down

0 comments on commit 78a253d

Please sign in to comment.