Improved the error handling from the client calls for the AmiiboLiveClient client in the library target.

This commit is contained in:
2025-10-02 03:12:27 +02:00
parent 8d0f00ec36
commit 903220050c
@@ -177,28 +177,18 @@ private extension AmiiboLiveClient {
let response: Operations.getAmiibos.Output
do {
response = try await client.getAmiibos(
.init(query: .init(
amiiboSeries: filter.series,
character: filter.gameCharacter,
gameseries: filter.gameSeries,
id: filter.identifier,
name: filter.name,
showgames: filter.showGames,
showusage: filter.showUsage,
_type: filter.type
))
)
} catch is CancellationError {
throw AmiiboServiceError.cancelled
} catch let error as ClientError {
if error.underlyingError is DecodingError {
throw AmiiboServiceError.decoding
} else {
throw AmiiboServiceError.unknown
}
response = try await client.getAmiibos(.init(query: .init(
amiiboSeries: filter.series,
character: filter.gameCharacter,
gameseries: filter.gameSeries,
id: filter.identifier,
name: filter.name,
showgames: filter.showGames,
showusage: filter.showUsage,
_type: filter.type
)))
} catch {
throw AmiiboServiceError.unknown
try handle(error: error)
}
switch response {
@@ -224,16 +214,12 @@ private extension AmiiboLiveClient {
let response: Operations.getAmiiboSeries.Output
do {
response = try await client.getAmiiboSeries(
.init(query: .init(
key: filter.key,
name: filter.name
))
)
} catch is CancellationError {
throw AmiiboServiceError.cancelled
response = try await client.getAmiiboSeries(.init(query: .init(
key: filter.key,
name: filter.name
)))
} catch {
throw AmiiboServiceError.unknown
try handle(error: error)
}
switch response {
@@ -263,16 +249,12 @@ private extension AmiiboLiveClient {
let response: Operations.getAmiiboTypes.Output
do {
response = try await client.getAmiiboTypes(
.init(query: .init(
key: filter.key,
name: filter.name
))
)
} catch is CancellationError {
throw AmiiboServiceError.cancelled
response = try await client.getAmiiboTypes(.init(query: .init(
key: filter.key,
name: filter.name
)))
} catch {
throw AmiiboServiceError.unknown
try handle(error: error)
}
switch response {
@@ -302,16 +284,12 @@ private extension AmiiboLiveClient {
let response: Operations.getGameCharacters.Output
do {
response = try await client.getGameCharacters(
.init(query: .init(
key: filter.key,
name: filter.name
))
)
} catch is CancellationError {
throw AmiiboServiceError.cancelled
response = try await client.getGameCharacters(.init(query: .init(
key: filter.key,
name: filter.name
)))
} catch {
throw AmiiboServiceError.unknown
try handle(error: error)
}
switch response {
@@ -341,16 +319,12 @@ private extension AmiiboLiveClient {
let response: Operations.getGameSeries.Output
do {
response = try await client.getGameSeries(
.init(query: .init(
key: filter.key,
name: filter.name
))
)
} catch is CancellationError {
throw AmiiboServiceError.cancelled
response = try await client.getGameSeries(.init(query: .init(
key: filter.key,
name: filter.name
)))
} catch {
throw AmiiboServiceError.unknown
try handle(error: error)
}
switch response {
@@ -378,10 +352,8 @@ private extension AmiiboLiveClient {
do {
response = try await client.getLastUpdated()
} catch is CancellationError {
throw AmiiboServiceError.cancelled
} catch {
throw AmiiboServiceError.unknown
try handle(error: error)
}
switch response {
@@ -395,6 +367,37 @@ private extension AmiiboLiveClient {
}
}
/// Maps a given error to a `AmiiboServiceError` error.
/// - Parameter error: An error to map.
/// - Throws: An ``AmiiboServiceError`` error.
func handle(error: any Error) throws -> Never {
switch error {
case is CancellationError:
throw AmiiboServiceError.cancelled
case let clientError as ClientError:
switch clientError.underlyingError {
case is DecodingError:
throw AmiiboServiceError.decoding
case let urlError as URLError:
switch urlError.code {
case .cannotFindHost,
.cannotConnectToHost,
.dnsLookupFailed,
.networkConnectionLost,
.notConnectedToInternet,
.timedOut:
throw AmiiboServiceError.notAvailable
default:
throw AmiiboServiceError.unknown
}
default:
throw AmiiboServiceError.unknown
}
default:
throw AmiiboServiceError.unknown
}
}
/// Retrieves a list of amiibo items from a wrapper container.
/// - Parameter wrapper: A wrapper container that either has an object or a list of items.
/// - Returns: A list of amiibo items, sorted by identifiers.
@@ -404,7 +407,6 @@ private extension AmiiboLiveClient {
switch wrapper.amiibo {
case let .Amiibo(object):
return [.init(object)]
case let .AmiiboList(list):
return list
.map { .init($0) }
@@ -424,7 +426,6 @@ private extension AmiiboLiveClient {
switch wrapper.amiibo {
case let .Tuple(payload):
return [.init(payload)]
case let .TupleList(list):
return list
.map { .init($0) }