Skip to content

Wrap registry login errors #6556

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions Sources/PackageRegistry/RegistryClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1319,20 +1319,22 @@ public final class RegistryClient: Cancellable {
let start = DispatchTime.now()
observabilityScope.emit(info: "logging-in into \(request.url)")
self.httpClient.execute(request, observabilityScope: observabilityScope, progress: nil) { result in
completion(
result.tryMap { response in
observabilityScope
.emit(
debug: "server response for \(request.url): \(response.statusCode) in \(start.distance(to: .now()).descriptionInSeconds)"
)
switch response.statusCode {
case 200:
return ()
default:
throw self.unexpectedStatusError(response, expectedStatus: [200])
}
switch result {
case .success(let response):
observabilityScope
.emit(
debug: "server response for \(request.url): \(response.statusCode) in \(start.distance(to: .now()).descriptionInSeconds)"
)
switch response.statusCode {
case 200:
return completion(.success(()))
default:
let error = self.unexpectedStatusError(response, expectedStatus: [200])
return completion(.failure(RegistryError.loginFailed(url: loginURL, error: error)))
}
)
case .failure(let error):
return completion(.failure(RegistryError.loginFailed(url: loginURL, error: error)))
}
}
}

Expand Down Expand Up @@ -1674,6 +1676,7 @@ public enum RegistryError: Error, CustomStringConvertible {
case unauthorized
case authenticationMethodNotSupported
case forbidden
case loginFailed(url: URL, error: Error)
case availabilityCheckFailed(registry: Registry, error: Error)
case registryNotAvailable(Registry)
case packageNotFound
Expand Down Expand Up @@ -1819,6 +1822,8 @@ public enum RegistryError: Error, CustomStringConvertible {
let previousVersion
):
return "the signing entity '\(String(describing: latest))' from \(registry) for \(package) version \(version) is different from the previously recorded value '\(previous)' for version \(previousVersion)"
case .loginFailed(let url, let error):
return "registry login using \(url) failed: \(error.interpolationDescription)"
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions Tests/PackageRegistryTests/RegistryClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3165,7 +3165,7 @@ final class RegistryClientTests: XCTestCase {
)

XCTAssertThrowsError(try registryClient.login(loginURL: loginURL)) { error in
guard case RegistryError.unauthorized = error else {
guard case RegistryError.loginFailed(_, _) = error else {
return XCTFail("Expected RegistryError.unauthorized, got \(error)")
}
}
Expand Down Expand Up @@ -3210,7 +3210,7 @@ final class RegistryClientTests: XCTestCase {
)

XCTAssertThrowsError(try registryClient.login(loginURL: loginURL)) { error in
guard case RegistryError.authenticationMethodNotSupported = error else {
guard case RegistryError.loginFailed = error else {
return XCTFail("Expected RegistryError.authenticationMethodNotSupported, got \(error)")
}
}
Expand Down