Skip to content
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

Better Handling of Apple Unauthorized errors #249

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
10 changes: 9 additions & 1 deletion Sources/XcodesKit/Downloader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,15 @@ public enum Downloader {
to: destination.url,
resumingWith: resumeData ?? persistedResumeData)
progressChanged(progress)
return promise.map { $0.saveLocation }
return promise.map { result in
/// If the operation is unauthorized, the download page redirects to https://developer.apple.com/unauthorized/
/// with 200 status. After that the html page is downloaded as a xip and subsequent unxipping fails
guard result.response.url?.lastPathComponent != "unauthorized" else {
throw XcodeInstaller.Error.unauthorized
}

return result.saveLocation
}
}
.tap { result in
self.persistOrCleanUpResumeData(at: resumeDataPath, for: result)
Expand Down
14 changes: 14 additions & 0 deletions Sources/XcodesKit/Environment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ public struct Shell {
process.standardError = stdErrPipe

var progress = Progress(totalUnitCount: 100)

// We hold on to the unauthorized status
// So that we can properly throw error from inside the promise
var unauthorized = false

let observer = NotificationCenter.default.addObserver(
forName: .NSFileHandleDataAvailable,
Expand All @@ -110,6 +114,13 @@ public struct Shell {
defer { handle.waitForDataInBackgroundAndNotify() }

let string = String(decoding: handle.availableData, as: UTF8.self)

/// If the operation is unauthorized, the download page redirects to https://developer.apple.com/unauthorized/
/// with 200 status. After that the html page is downloaded as a xip and subsequent unxipping fails
if !unauthorized && string.contains("Redirecting to https://developer.apple.com/unauthorized/") {
unauthorized = true
}

let regex = try! NSRegularExpression(pattern: #"((?<percent>\d+)%\))"#)
let range = NSRange(location: 0, length: string.utf16.count)

Expand Down Expand Up @@ -144,6 +155,9 @@ public struct Shell {
return seal.reject(Process.PMKError.execution(process: process, standardOutput: "", standardError: ""))
}
}
guard !unauthorized else {
return seal.reject(XcodeInstaller.Error.unauthorized)
}
seal.fulfill(())
}
}
Expand Down
11 changes: 11 additions & 0 deletions Sources/XcodesKit/Promise+.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ func attemptResumableTask<T>(
attempts < maximumRetryCount,
let resumeData = (error as NSError).userInfo[NSURLSessionDownloadTaskResumeData] as? Data
else { throw error }

// Don't retry unauthorized errors because it won't change the outcome
if case XcodeInstaller.Error.unauthorized = error {
throw error
}

return after(delayBeforeRetry).then(on: nil) { attempt(with: resumeData) }
}
Expand All @@ -33,6 +38,12 @@ func attemptRetryableTask<T>(
attempts += 1
return body().recover { error -> Promise<T> in
guard attempts < maximumRetryCount else { throw error }

// Don't retry unauthorized errors because it won't change the outcome
if case XcodeInstaller.Error.unauthorized = error {
throw error
}

return after(delayBeforeRetry).then(on: nil) { attempt() }
}
}
Expand Down
7 changes: 7 additions & 0 deletions Sources/XcodesKit/XcodeInstaller.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public final class XcodeInstaller {
case versionAlreadyInstalled(InstalledXcode)
case invalidVersion(String)
case versionNotInstalled(Version)
case unauthorized

public var errorDescription: String? {
switch self {
Expand Down Expand Up @@ -70,6 +71,12 @@ public final class XcodeInstaller {
return "\(version) is not a valid version number."
case let .versionNotInstalled(version):
return "\(version.appleDescription) is not installed."
case .unauthorized:
return """
Received 403: Unauthorized. This can happen when either:
1. Apple Developer Terms and Conditions were not accepted at https://developer.apple.com/
2. Apple ID authorization was revoked for some other reason
"""
}
}
}
Expand Down