Skip to content

Commit

Permalink
Output to stderr responses from Apple endpoints that are unparsable a…
Browse files Browse the repository at this point in the history
…s JSON

Resolves #536

Signed-off-by: Ross Goldberg <[email protected]>
  • Loading branch information
rgoldberg committed Oct 5, 2024
1 parent 622a154 commit 7e9e3ba
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
2 changes: 1 addition & 1 deletion 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 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")
}
}

0 comments on commit 7e9e3ba

Please sign in to comment.