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

Log ToolchainError with response contents #435

Merged
merged 1 commit into from
May 17, 2024
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
12 changes: 9 additions & 3 deletions Sources/SwiftToolchain/ToolchainError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import Foundation
import CartonHelpers

enum ToolchainError: Error, CustomStringConvertible {
case directoryDoesNotExist(AbsolutePath)
case invalidInstallationArchive(AbsolutePath)
case invalidVersion(version: String)
case invalidResponse(url: String, status: Int)
case notHTTPURLResponse(url: String)
case invalidResponse(url: String, status: Int, body: Data)
case unsupportedOperatingSystem
case noInstallationDirectory(path: String)

Expand All @@ -30,8 +32,12 @@ enum ToolchainError: Error, CustomStringConvertible {
return "Invalid toolchain/SDK archive was installed at path \(path)"
case let .invalidVersion(version):
return "Invalid version \(version)"
case let .invalidResponse(url: url, status: status):
return "Response from \(url) had invalid status \(status) or didn't contain body"
case let .notHTTPURLResponse(url: url):
return "Response from \(url) is not HTTPURLResponse"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

異なる状況で invalidResponse を併用しているのがわかりづらかったので別のエラーに分離しました。

case let .invalidResponse(url: url, status: status, body: body):
var t = "Response from \(url) had invalid status \(status) with a body of \(body.count) bytes: "
t += String(decoding: body, as: UTF8.self)
return t
case .unsupportedOperatingSystem:
return "This version of the operating system is not supported"
case let .noInstallationDirectory(path):
Expand Down
6 changes: 4 additions & 2 deletions Sources/SwiftToolchain/ToolchainManagement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,12 @@ public class ToolchainSystem {
let request = URLRequest(url: URL(string: releaseURL)!)
let (data, response) = try await URLSession.shared.data(for: request)
guard let httpResponse = response as? HTTPURLResponse else {
throw ToolchainError.invalidResponse(url: releaseURL, status: -1)
throw ToolchainError.notHTTPURLResponse(url: releaseURL)
}
guard 200..<300 ~= httpResponse.statusCode else {
throw ToolchainError.invalidResponse(url: releaseURL, status: httpResponse.statusCode)
throw ToolchainError.invalidResponse(
url: releaseURL, status: httpResponse.statusCode, body: data
)
}
terminal.write("Response contained body, parsing it now...\n", inColor: .green)

Expand Down
Loading