Map wrapped cancellation in the Amiibo live client to the cancelled error.
This commit is contained in:
1 parent
44bfaca553
commit
51b1245ec3
4 files changed
+102
-2
No files matched your search
+4
-1
@@ -35,6 +35,7 @@ let package = Package(
|
|||||||
.package(url: "https://github.com/apple/swift-openapi-generator.git", from: "1.11.0"),
|
.package(url: "https://github.com/apple/swift-openapi-generator.git", from: "1.11.0"),
|
||||||
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.5.0"),
|
.package(url: "https://github.com/apple/swift-openapi-runtime", from: "1.5.0"),
|
||||||
.package(url: "https://github.com/apple/swift-openapi-urlsession", from: "1.0.2"),
|
.package(url: "https://github.com/apple/swift-openapi-urlsession", from: "1.0.2"),
|
||||||
|
.package(url: "https://github.com/apple/swift-http-types", from: "1.0.0"),
|
||||||
.package(url: "https://github.com/swiftlang/swift-docc-plugin", from: "1.1.0"),
|
.package(url: "https://github.com/swiftlang/swift-docc-plugin", from: "1.1.0"),
|
||||||
],
|
],
|
||||||
targets: [
|
targets: [
|
||||||
@@ -52,7 +53,9 @@ let package = Package(
|
|||||||
.testTarget(
|
.testTarget(
|
||||||
name: AmiiboService.test,
|
name: AmiiboService.test,
|
||||||
dependencies: [
|
dependencies: [
|
||||||
.byName(name: AmiiboService.target)
|
.byName(name: AmiiboService.target),
|
||||||
|
.product(name: "HTTPTypes", package: "swift-http-types"),
|
||||||
|
.product(name: "OpenAPIRuntime", package: "swift-openapi-runtime")
|
||||||
],
|
],
|
||||||
path: "Tests/AmiiboService"
|
path: "Tests/AmiiboService"
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -370,7 +370,10 @@ private extension AmiiboLiveClient {
|
|||||||
case is CancellationError:
|
case is CancellationError:
|
||||||
throw AmiiboServiceError.cancelled
|
throw AmiiboServiceError.cancelled
|
||||||
case let clientError as ClientError:
|
case let clientError as ClientError:
|
||||||
|
// The underlying client wraps every error, including cancellation, in a `ClientError`.
|
||||||
switch clientError.underlyingError {
|
switch clientError.underlyingError {
|
||||||
|
case is CancellationError:
|
||||||
|
throw AmiiboServiceError.cancelled
|
||||||
case is DecodingError:
|
case is DecodingError:
|
||||||
throw AmiiboServiceError.decoding
|
throw AmiiboServiceError.decoding
|
||||||
case let urlError as URLError:
|
case let urlError as URLError:
|
||||||
|
|||||||
@@ -42,5 +42,51 @@ struct AmiiboLiveClientTests {
|
|||||||
// THEN
|
// THEN
|
||||||
#expect(url == expected)
|
#expect(url == expected)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MARK: Error mapping tests
|
||||||
|
|
||||||
|
@Test(
|
||||||
|
"maps an error thrown by the transport to the matching service error",
|
||||||
|
arguments: zip(
|
||||||
|
TransportErrors.thrown,
|
||||||
|
TransportErrors.expected
|
||||||
|
)
|
||||||
|
)
|
||||||
|
func mapsTransportError(
|
||||||
|
thrown: any Error,
|
||||||
|
expected: AmiiboServiceError
|
||||||
|
) async {
|
||||||
|
// GIVEN
|
||||||
|
let client = AmiiboLiveClient(transport: ThrowingTransport(error: thrown))
|
||||||
|
|
||||||
|
// WHEN
|
||||||
|
// THEN
|
||||||
|
await #expect(throws: expected) {
|
||||||
|
try await client.getAmiibos(by: .init())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: - Arguments
|
||||||
|
|
||||||
|
private enum TransportErrors {
|
||||||
|
/// Errors thrown by the transport.
|
||||||
|
static let thrown: [any Error] = [
|
||||||
|
CancellationError(),
|
||||||
|
URLError(.cancelled),
|
||||||
|
URLError(.cannotParseResponse),
|
||||||
|
URLError(.notConnectedToInternet),
|
||||||
|
URLError(.timedOut),
|
||||||
|
URLError(.badURL)
|
||||||
|
]
|
||||||
|
/// The service error expected for each thrown error.
|
||||||
|
static let expected: [AmiiboServiceError] = [
|
||||||
|
.cancelled,
|
||||||
|
.cancelled,
|
||||||
|
.decoding,
|
||||||
|
.notAvailable,
|
||||||
|
.notAvailable,
|
||||||
|
.unknown(String(describing: URLError(.badURL)))
|
||||||
|
]
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
// ===----------------------------------------------------------------------===
|
||||||
|
//
|
||||||
|
// This source file is part of the Amiibo Service open source project
|
||||||
|
//
|
||||||
|
// Copyright (c) 2026 Röck+Cöde VoF. and the Amiibo Service project authors
|
||||||
|
// Licensed under Apache license v2.0
|
||||||
|
//
|
||||||
|
// See LICENSE for license information
|
||||||
|
// See CONTRIBUTORS for the list of Amiibo Service project authors
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
//
|
||||||
|
// ===----------------------------------------------------------------------===
|
||||||
|
|
||||||
|
import Foundation
|
||||||
|
import HTTPTypes
|
||||||
|
import OpenAPIRuntime
|
||||||
|
|
||||||
|
/// A transport that throws a given error instead of sending the request, for testing purposes.
|
||||||
|
///
|
||||||
|
/// The error reaches the live client wrapped in a `ClientError`, as it would from a real transport.
|
||||||
|
struct ThrowingTransport: ClientTransport {
|
||||||
|
|
||||||
|
// MARK: Properties
|
||||||
|
|
||||||
|
/// The error to throw.
|
||||||
|
private let error: any Error
|
||||||
|
|
||||||
|
// MARK: Initializers
|
||||||
|
|
||||||
|
/// Initializes this transport.
|
||||||
|
/// - Parameter error: An error to throw.
|
||||||
|
init(error: any Error) {
|
||||||
|
self.error = error
|
||||||
|
}
|
||||||
|
|
||||||
|
// MARK: Functions
|
||||||
|
|
||||||
|
func send(
|
||||||
|
_ request: HTTPRequest,
|
||||||
|
body: HTTPBody?,
|
||||||
|
baseURL: URL,
|
||||||
|
operationID: String
|
||||||
|
) async throws -> (HTTPResponse, HTTPBody?) {
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in new issue
Block a user