Merge pull request #550 from rgoldberg/536-output-responses-unparsable-as-json

Output to stderr responses from Apple endpoints that are unparsable as JSON
This commit is contained in:
Ross Goldberg 2024-10-13 22:20:51 -04:00 committed by GitHub
commit 1bebde9e4a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 14 additions and 6 deletions

View file

@ -108,7 +108,7 @@ class MasStoreSearch: StoreSearch {
do {
return try JSONDecoder().decode(SearchResultList.self, from: data).results
} catch {
throw MASError.jsonParsing(error: error as NSError)
throw MASError.jsonParsing(data: data)
}
}
}

View file

@ -28,7 +28,7 @@ public enum MASError: Error, Equatable {
case uninstallFailed
case noData
case jsonParsing(error: NSError?)
case jsonParsing(data: Data?)
}
// MARK: - CustomStringConvertible
@ -93,8 +93,16 @@ extension MASError: CustomStringConvertible {
case .noData:
return "Service did not return data"
case .jsonParsing:
return "Unable to parse response JSON"
case .jsonParsing(let data):
if let data {
if let unparsable = String(data: data, encoding: .utf8) {
return "Unable to parse response as JSON: \n\(unparsable)"
} else {
return "Received defective response"
}
} else {
return "Received empty response"
}
}
}
}

View file

@ -122,7 +122,7 @@ class MASErrorTestCase: XCTestCase {
}
func testJsonParsing() {
error = .jsonParsing(error: nil)
XCTAssertEqual(error.description, "Unable to parse response JSON")
error = .jsonParsing(data: nil)
XCTAssertEqual(error.description, "Received empty response")
}
}