Skip to content

Commit 85366e6

Browse files
dblythycbaker6TomWFox
authored
feat: Add invalid struct error (#238)
* new: allow for custom error codes from Parse Cloud * Update APICommandTests.swift * Update ParseError.swift * fix lint * Decode parse otherCode error and add tests * Update Sources/ParseSwift/Types/ParseError.swift Co-authored-by: Tom Fox <[email protected]> * add guard * Update Sources/ParseSwift/Types/ParseError.swift Co-authored-by: Corey <[email protected]> * Update Tests/ParseSwiftTests/ParseErrorTests.swift Co-authored-by: Corey <[email protected]> * Update CHANGELOG.md * add invalid struct * change line length * Update URLSession+extensions.swift * change line length * Update ParseError.swift * fix lint * Revert "fix lint" This reverts commit 889b3af. * fix lint * make changes * remove invalid struct * Update URLSession+extensions.swift * Update ParseQueryTests.swift * Update ParseQueryTests.swift * revert changes * Update ParseErrorTests.swift * Update ParseQueryTests.swift * update podspec and change log * remove localized error Co-authored-by: Corey <[email protected]> Co-authored-by: Tom Fox <[email protected]>
1 parent 7aeef06 commit 85366e6

File tree

6 files changed

+29
-1
lines changed

6 files changed

+29
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
__Improvements__
1111
- 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).
12+
- 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).
1213

1314
### 1.10.4
1415
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/1.10.3...1.10.4)

ParseSwift.podtemplate

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ Pod::Spec.new do |s|
1414
s.osx.deployment_target = "10.13"
1515
s.tvos.deployment_target = "12.0"
1616
s.watchos.deployment_target = "5.0"
17-
s.swift_versions = ['5.1', '5.2', '5.3', '5.4']
17+
s.swift_versions = ['5.1', '5.2', '5.3', '5.4', '5.5']
1818
s.source_files = "Sources/ParseSwift/**/*.swift"
1919
s.license = {
2020
:type => "MIT",

Sources/ParseSwift/API/URLSession+extensions.swift

+5
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,11 @@ extension URLSession {
6666
let json = try? JSONSerialization
6767
.data(withJSONObject: responseData,
6868
options: .prettyPrinted) else {
69+
let nsError = error as NSError
70+
if nsError.code == 4865,
71+
let description = nsError.userInfo["NSDebugDescription"] {
72+
return .failure(ParseError(code: .unknownError, message: "Invalid struct: \(description)"))
73+
}
6974
return .failure(ParseError(code: .unknownError,
7075
// swiftlint:disable:next line_length
7176
message: "Error decoding parse-server response: \(response) with error: \(error.localizedDescription) Format: \(String(describing: String(data: responseData, encoding: .utf8)))"))

Sources/ParseSwift/Types/ParseError.swift

+7
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,13 @@ extension ParseError: CustomStringConvertible {
407407
}
408408
}
409409

410+
// MARK: LocalizedError
411+
extension ParseError: LocalizedError {
412+
public var errorDescription: String? {
413+
debugDescription
414+
}
415+
}
416+
410417
// MARK: Compare Errors
411418
public extension Error {
412419

Tests/ParseSwiftTests/ParseErrorTests.swift

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class ParseErrorTests: XCTestCase {
4646
XCTAssertEqual(decoded.message, message)
4747
XCTAssertEqual(decoded.debugDescription, "ParseError code=\(code) error=\(message)")
4848
XCTAssertEqual(decoded.description, "ParseError code=\(code) error=\(message)")
49+
XCTAssertEqual(decoded.errorDescription, "ParseError code=\(code) error=\(message)")
4950
}
5051

5152
func testEncodeOther() throws {

Tests/ParseSwiftTests/ParseQueryTests.swift

+14
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,20 @@ class ParseQueryTests: XCTestCase { // swiftlint:disable:this type_body_length
726726
}
727727

728728
let query = GameScore.query()
729+
do {
730+
_ = try query.first(options: [])
731+
XCTFail("Should have thrown error")
732+
} catch {
733+
guard let error = error as? ParseError else {
734+
XCTFail("Should have casted as ParseError")
735+
return
736+
}
737+
#if !os(Linux) && !os(Android)
738+
// swiftlint:disable:next line_length
739+
XCTAssertEqual(error.message, "Invalid struct: No value associated with key CodingKeys(stringValue: \"score\", intValue: nil) (\"score\").")
740+
XCTAssertEqual(error.code, .unknownError)
741+
#endif
742+
}
729743
XCTAssertThrowsError(try query.first(options: []))
730744
}
731745

0 commit comments

Comments
 (0)