Skip to content

Improve error handling for 'package-registry login' command #6286

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 4 commits into from
Mar 16, 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
18 changes: 13 additions & 5 deletions Sources/PackageRegistryTool/PackageRegistryTool+Auth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,19 @@ extension SwiftPackageRegistryTool {
private static let PLACEHOLDER_TOKEN_USER = "token"

func run(_ swiftTool: SwiftTool) throws {
// We need to be able to read/write credentials
// Make sure credentials store is available before proceeding
let authorizationProvider: AuthorizationProvider?
do {
authorizationProvider = try swiftTool.getRegistryAuthorizationProvider()
} catch {
throw ValidationError.invalidCredentialStore(error)
}

guard let authorizationProvider = authorizationProvider else {
throw ValidationError.unknownCredentialStore
}

let configuration = try getRegistriesConfig(swiftTool)

// compute and validate registry URL
Expand All @@ -101,11 +114,6 @@ extension SwiftPackageRegistryTool {
throw ValidationError.invalidURL(registryURL)
}

// We need to be able to read/write credentials
guard let authorizationProvider = try swiftTool.getRegistryAuthorizationProvider() else {
throw ValidationError.unknownCredentialStore
}

let authenticationType: RegistryConfiguration.AuthenticationType
let storeUsername: String
let storePassword: String
Expand Down
13 changes: 8 additions & 5 deletions Sources/PackageRegistryTool/PackageRegistryTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public struct SwiftPackageRegistryTool: ParsableCommand {
abstract: "Interact with package registry and manage related configuration",
discussion: "SEE ALSO: swift package",
version: SwiftVersion.current.completeDisplayString,
subcommands:[
subcommands: [
Set.self,
Unset.self,
Login.self,
Expand Down Expand Up @@ -137,6 +137,7 @@ public struct SwiftPackageRegistryTool: ParsableCommand {
case invalidPackageIdentity(PackageIdentity)
case unknownRegistry
case unknownCredentialStore
case invalidCredentialStore(Error)
}

static func getRegistriesConfig(_ swiftTool: SwiftTool) throws -> Workspace.Configuration.Registries {
Expand Down Expand Up @@ -172,13 +173,15 @@ extension SwiftPackageRegistryTool.ValidationError: CustomStringConvertible {
var description: String {
switch self {
case .invalidURL(let url):
return "Invalid URL: \(url)"
return "invalid URL: \(url)"
case .invalidPackageIdentity(let identity):
return "Invalid package identifier '\(identity)'"
return "invalid package identifier '\(identity)'"
case .unknownRegistry:
return "Unknown registry, is one configured?"
return "unknown registry, is one configured?"
case .unknownCredentialStore:
return "No credential store available"
return "no credential store available"
case .invalidCredentialStore(let error):
return "credential store is invalid: \(error)"
}
}
}
2 changes: 1 addition & 1 deletion Sources/Workspace/Workspace+Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ extension Workspace.Configuration {
switch self.netrc {
case .custom(let path):
guard fileSystem.exists(path) else {
throw StringError("Did not find netrc file at \(path).")
throw StringError("did not find netrc file at \(path)")
}
providers.append(try NetrcAuthorizationProvider(path: path, fileSystem: fileSystem))
case .user:
Expand Down
2 changes: 1 addition & 1 deletion Tests/CommandsTests/SwiftToolTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ final class SwiftToolTests: CommandsTestCase {
// delete it
try localFileSystem.removeFileTree(customPath)
XCTAssertThrowsError(try tool.getRegistryAuthorizationProvider(), "error expected") { error in
XCTAssertEqual(error as? StringError, StringError("Did not find netrc file at \(customPath)."))
XCTAssertEqual(error as? StringError, StringError("did not find netrc file at \(customPath)"))
}
#endif
}
Expand Down
2 changes: 1 addition & 1 deletion Tests/WorkspaceTests/AuthorizationProviderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ final class AuthorizationProviderTests: XCTestCase {
// delete it
try fileSystem.removeFileTree(customPath)
XCTAssertThrowsError(try configuration.makeRegistryAuthorizationProvider(fileSystem: fileSystem, observabilityScope: observability.topScope), "error expected") { error in
XCTAssertEqual(error as? StringError, StringError("Did not find netrc file at \(customPath)."))
XCTAssertEqual(error as? StringError, StringError("did not find netrc file at \(customPath)"))
}
}

Expand Down