Skip to content
Merged
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
23 changes: 19 additions & 4 deletions Sources/ContainerRegistry/RegistryClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public enum RegistryClientError: Error {
case invalidUploadLocation(String)
case invalidDigestAlgorithm(String)
case digestMismatch(expected: String, registry: String)
case unexpectedRegistryResponse(status: Int, body: String)
}

extension RegistryClientError: CustomStringConvertible {
Expand All @@ -38,6 +39,8 @@ extension RegistryClientError: CustomStringConvertible {
case let .invalidDigestAlgorithm(digest): return "Invalid or unsupported digest algorithm: \(digest)"
case let .digestMismatch(expected, registry):
return "Digest mismatch: expected \(expected), registry sent \(registry)"
case let .unexpectedRegistryResponse(status, body):
return "Registry returned HTTP \(status): \(body)"
}
}
}
Expand Down Expand Up @@ -369,8 +372,14 @@ extension RegistryClient {
} catch HTTPClientError.unexpectedStatusCode(let status, _, let .some(responseData))
where errors.contains(status)
{
let decoded = try decoder.decode(DistributionErrors.self, from: responseData)
throw decoded
// Try to decode as JSON; if that fails, throw a generic error with the raw response body
do {
let decoded = try decoder.decode(DistributionErrors.self, from: responseData)
throw decoded
} catch is DecodingError {
let bodyText = String(data: responseData, encoding: .utf8) ?? "<non-UTF8 response>"
throw RegistryClientError.unexpectedRegistryResponse(status: status.code, body: bodyText)
}
}
}

Expand Down Expand Up @@ -410,8 +419,14 @@ extension RegistryClient {
} catch HTTPClientError.unexpectedStatusCode(let status, _, let .some(responseData))
where errors.contains(status)
{
let decoded = try decoder.decode(DistributionErrors.self, from: responseData)
throw decoded
// Try to decode as JSON; if that fails, throw a generic error with the raw response body
do {
let decoded = try decoder.decode(DistributionErrors.self, from: responseData)
throw decoded
} catch is DecodingError {
let bodyText = String(data: responseData, encoding: .utf8) ?? "<non-UTF8 response>"
throw RegistryClientError.unexpectedRegistryResponse(status: status.code, body: bodyText)
}
}
}

Expand Down