Skip to content

Commit c16252e

Browse files
authored
improve error handling of availability check (#6506)
motvation: more verbose error message when availability check fails changes: wrap availability check error handling with explicit error rdar://108418554
1 parent 97e099b commit c16252e

File tree

1 file changed

+35
-24
lines changed

1 file changed

+35
-24
lines changed

Sources/PackageRegistry/RegistryClient.swift

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -361,12 +361,13 @@ public final class RegistryClient: Cancellable {
361361
}
362362

363363
SignatureValidation.extractSigningEntity(
364-
signature: [UInt8](signatureData),
365-
signatureFormat: signatureFormat,
366-
configuration: configuration,
367-
fileSystem: fileSystem,
368-
completion: wrappedCompletion
369-
) }
364+
signature: [UInt8](signatureData),
365+
signatureFormat: signatureFormat,
366+
configuration: configuration,
367+
fileSystem: fileSystem,
368+
completion: wrappedCompletion
369+
)
370+
}
370371
}
371372
)
372373
},
@@ -1534,25 +1535,26 @@ public final class RegistryClient: Cancellable {
15341535
let start = DispatchTime.now()
15351536
observabilityScope.emit(info: "checking availability of \(registry.url) using \(request.url)")
15361537
self.httpClient.execute(request, observabilityScope: observabilityScope, progress: nil) { result in
1537-
completion(
1538-
result.tryMap { response in
1539-
observabilityScope
1540-
.emit(
1541-
debug: "server response for \(request.url): \(response.statusCode) in \(start.distance(to: .now()).descriptionInSeconds)"
1542-
)
1543-
switch response.statusCode {
1544-
case 200:
1545-
return .available
1546-
case let value where AvailabilityStatus.unavailableStatusCodes.contains(value):
1547-
return .unavailable
1548-
default:
1549-
if let error = try? response.parseError(decoder: self.jsonDecoder) {
1550-
return .error(error.detail)
1551-
}
1552-
return .error("unknown server error (\(response.statusCode))")
1538+
switch result {
1539+
case .success(let response):
1540+
observabilityScope
1541+
.emit(
1542+
debug: "server response for \(request.url): \(response.statusCode) in \(start.distance(to: .now()).descriptionInSeconds)"
1543+
)
1544+
switch response.statusCode {
1545+
case 200:
1546+
return completion(.success(.available))
1547+
case let value where AvailabilityStatus.unavailableStatusCodes.contains(value):
1548+
return completion(.success(.unavailable))
1549+
default:
1550+
if let error = try? response.parseError(decoder: self.jsonDecoder) {
1551+
return completion(.success(.error(error.detail)))
15531552
}
1553+
return completion(.success(.error("unknown server error (\(response.statusCode))")))
15541554
}
1555-
)
1555+
case .failure(let error):
1556+
return completion(.failure(RegistryError.availabilityCheckFailed(registry: registry, error: error)))
1557+
}
15561558
}
15571559
}
15581560

@@ -1671,6 +1673,7 @@ public enum RegistryError: Error, CustomStringConvertible {
16711673
case unauthorized
16721674
case authenticationMethodNotSupported
16731675
case forbidden
1676+
case availabilityCheckFailed(registry: Registry, error: Error)
16741677
case registryNotAvailable(Registry)
16751678
case packageNotFound
16761679
case packageVersionNotFound
@@ -1770,6 +1773,8 @@ public enum RegistryError: Error, CustomStringConvertible {
17701773
return "authentication method not supported"
17711774
case .forbidden:
17721775
return "forbidden"
1776+
case .availabilityCheckFailed(let registry, let error):
1777+
return "failed checking availability of registry at '\(registry.url)': \(error.interpolationDescription)"
17731778
case .registryNotAvailable(let registry):
17741779
return "registry at '\(registry.url)' is not available at this time, please try again later"
17751780
case .packageNotFound:
@@ -1870,7 +1875,13 @@ extension RegistryClient {
18701875
public let signing: Signing?
18711876
public let signingEntity: SigningEntity?
18721877

1873-
public init(name: String, type: String, checksum: String?, signing: Signing?, signingEntity: SigningEntity?) {
1878+
public init(
1879+
name: String,
1880+
type: String,
1881+
checksum: String?,
1882+
signing: Signing?,
1883+
signingEntity: SigningEntity?
1884+
) {
18741885
self.name = name
18751886
self.type = type
18761887
self.checksum = checksum

0 commit comments

Comments
 (0)