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

Output to stderr responses from Apple endpoints that are unparsable as JSON #537

Closed
Closed
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
7 changes: 4 additions & 3 deletions Sources/MasKit/Controllers/MasStoreSearch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand All @@ -122,8 +122,9 @@ class MasStoreSearch: StoreSearch {
firstly {
networkManager.loadData(from: pageUrl)
}.map { data in
let html = String(decoding: data, as: UTF8.self)
guard let capture = MasStoreSearch.appVersionExpression.firstMatch(in: html)?.captures[0],
let html = String(data: data, encoding: .utf8)
guard let html,
let capture = MasStoreSearch.appVersionExpression.firstMatch(in: html)?.captures[0],
let version = Version(tolerant: capture)
else {
return nil
Expand Down
14 changes: 11 additions & 3 deletions Sources/MasKit/Errors/MASError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public enum MASError: Error, Equatable {
case uninstallFailed

case noData
case jsonParsing(error: NSError?)
case jsonParsing(data: Data?)
}

// MARK: - CustomStringConvertible
Expand Down Expand Up @@ -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"
}
}
}
}
4 changes: 2 additions & 2 deletions Tests/MasKitTests/Errors/MASErrorTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
Loading