diff --git a/CHANGELOG.md b/CHANGELOG.md index 3dcedc664..09758fa0c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ __Improvements__ - Added `operation` for `set` and `forceSet`, used for single key updates ([#248](https://github.com/parse-community/Parse-Swift/pull/248)), thanks to [Daniel Blyth](https://github.com/dblythy) and [Corey Baker](https://github.com/cbaker6). +- Add more detail to invalid struct errors ([#238](https://github.com/parse-community/Parse-Swift/pull/238)), thanks to [Daniel Blyth](https://github.com/dblythy). ### 1.10.4 [Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.10.3...1.10.4) diff --git a/ParseSwift.podtemplate b/ParseSwift.podtemplate index c2e752dca..03382c518 100644 --- a/ParseSwift.podtemplate +++ b/ParseSwift.podtemplate @@ -14,7 +14,7 @@ Pod::Spec.new do |s| s.osx.deployment_target = "10.13" s.tvos.deployment_target = "12.0" s.watchos.deployment_target = "5.0" - s.swift_versions = ['5.1', '5.2', '5.3', '5.4'] + s.swift_versions = ['5.1', '5.2', '5.3', '5.4', '5.5'] s.source_files = "Sources/ParseSwift/**/*.swift" s.license = { :type => "MIT", diff --git a/Sources/ParseSwift/API/URLSession+extensions.swift b/Sources/ParseSwift/API/URLSession+extensions.swift index 9be9eb90c..3df1e2f0a 100755 --- a/Sources/ParseSwift/API/URLSession+extensions.swift +++ b/Sources/ParseSwift/API/URLSession+extensions.swift @@ -66,6 +66,11 @@ extension URLSession { let json = try? JSONSerialization .data(withJSONObject: responseData, options: .prettyPrinted) else { + let nsError = error as NSError + if nsError.code == 4865, + let description = nsError.userInfo["NSDebugDescription"] { + return .failure(ParseError(code: .unknownError, message: "Invalid struct: \(description)")) + } return .failure(ParseError(code: .unknownError, // swiftlint:disable:next line_length message: "Error decoding parse-server response: \(response) with error: \(error.localizedDescription) Format: \(String(describing: String(data: responseData, encoding: .utf8)))")) diff --git a/Sources/ParseSwift/Types/ParseError.swift b/Sources/ParseSwift/Types/ParseError.swift index 97d1eee96..e0e0844de 100644 --- a/Sources/ParseSwift/Types/ParseError.swift +++ b/Sources/ParseSwift/Types/ParseError.swift @@ -407,6 +407,13 @@ extension ParseError: CustomStringConvertible { } } +// MARK: LocalizedError +extension ParseError: LocalizedError { + public var errorDescription: String? { + debugDescription + } +} + // MARK: Compare Errors public extension Error { diff --git a/Tests/ParseSwiftTests/ParseErrorTests.swift b/Tests/ParseSwiftTests/ParseErrorTests.swift index f091fd102..b229d1edf 100644 --- a/Tests/ParseSwiftTests/ParseErrorTests.swift +++ b/Tests/ParseSwiftTests/ParseErrorTests.swift @@ -46,6 +46,7 @@ class ParseErrorTests: XCTestCase { XCTAssertEqual(decoded.message, message) XCTAssertEqual(decoded.debugDescription, "ParseError code=\(code) error=\(message)") XCTAssertEqual(decoded.description, "ParseError code=\(code) error=\(message)") + XCTAssertEqual(decoded.errorDescription, "ParseError code=\(code) error=\(message)") } func testEncodeOther() throws { diff --git a/Tests/ParseSwiftTests/ParseQueryTests.swift b/Tests/ParseSwiftTests/ParseQueryTests.swift index 832d20106..85fcb22fd 100644 --- a/Tests/ParseSwiftTests/ParseQueryTests.swift +++ b/Tests/ParseSwiftTests/ParseQueryTests.swift @@ -726,6 +726,20 @@ class ParseQueryTests: XCTestCase { // swiftlint:disable:this type_body_length } let query = GameScore.query() + do { + _ = try query.first(options: []) + XCTFail("Should have thrown error") + } catch { + guard let error = error as? ParseError else { + XCTFail("Should have casted as ParseError") + return + } + #if !os(Linux) && !os(Android) + // swiftlint:disable:next line_length + XCTAssertEqual(error.message, "Invalid struct: No value associated with key CodingKeys(stringValue: \"score\", intValue: nil) (\"score\").") + XCTAssertEqual(error.code, .unknownError) + #endif + } XCTAssertThrowsError(try query.first(options: [])) }