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

Change parseEntity to not wrap errors thrown by Decoders and Decodables. #29

Closed
wants to merge 1 commit into from
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
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ All notable changes to this project will be documented in this file.

#### 2.x Releases

* `2.2.x` Releases = [2.2.0](#220) | [2.2.1](#221)
* `2.2.x` Releases = [2.2.0](#220) | [2.2.1](#221) | [2.2.2](#222)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's revert this since there's going to be another PR going into 2.2.2.

* `2.1.x` Releases = [2.1.0](#210)
* `2.0.x` Releases = [2.0.0](#200)

Expand All @@ -16,6 +16,13 @@ All notable changes to this project will be documented in this file.

---

## [2.2.2](https://github.com/Nike-Inc/Elevate/releases/tag/2.2.2)

Released on 2017-06-22. All issues associated with this milestone can be found using this
[filter](https://github.com/Nike-Inc/Elevate/milestone/5?closed=1).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's switch this over to master and add a commit message for these changes.


---

## [2.2.1](https://github.com/Nike-Inc/Elevate/releases/tag/2.2.1)

Released on 2017-02-09. All issues associated with this milestone can be found using this
Expand Down
9 changes: 7 additions & 2 deletions Source/Parser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,15 @@ public class Parser {
/// - closure: Defines the property list for the parser via the passed in `Schema` instance.
///
/// - Returns: The parsed entity as a Dictionary.
///
/// - Throws: `ParserError` for parsing related errors or `Error`s thrown by custom `Decodable` and `Decoder` implementations.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can condense this wording a bit.

public class func parseEntity(data: Data, closure: (Schema) -> Void) throws -> [String: Any] {
let result: [String: Any]
let json: Any

// A JSONSerialization error will be converted to ParserError
do {
let json = try JSONSerialization.jsonObject(with: data, options: [])
result = try parseEntity(json: json, closure: closure)
json = try JSONSerialization.jsonObject(with: data, options: [])
} catch {
if error is ParserError {
throw error
Expand All @@ -60,6 +63,8 @@ public class Parser {
}
}

result = try parseEntity(json: json, closure: closure)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can just get rid of the do-catch here.


return result
}

Expand Down
19 changes: 19 additions & 0 deletions Tests/DecodableTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,25 @@ class DecodableTestCase: BaseTestCase {
decodableErrorTest(type: [String: String].self, value: "1")
}

func testThatThrownCustomErrorsCanBeCaught() {
do {
// Given
let data = loadJSONDataForFileNamed("PropertyTypesTest")

// When
let _: ErrorThrowingDecodable = try Elevate.decodeObject(from: data)

XCTFail("Decoding unexpectedly succeeded.")
} catch let error as NSError {
// Then
XCTAssertEqual(error.domain, "Decodable Test Error", "Error domain did not match expected value.")
XCTAssertEqual(error.code, 42, "Error code did not match expected value.")
} catch {
XCTFail("Parser error was of incorrect type.")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should switch over to using the XCTAssertThrowsError APIs here.

}
}

// MARK: - Private - Helper Methods

private func decodableErrorTest(type: Decodable.Type, value: Any) {
Expand Down
19 changes: 19 additions & 0 deletions Tests/DecoderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,23 @@ class DateDecoderTestCase: BaseTestCase {
XCTFail("Parser error was of incorrect type")
}
}

func testThatThrownCustomErrorsCanBeCaught() {
do {
// Given
let data = loadJSONDataForFileNamed("PropertyTypesTest")

// When
let _: ErrorThrowingDecodable = try Elevate.decodeObject(from: data, with: ErrorThrowingDecoder())

XCTFail("Decoding unexpectedly succeeded.")
} catch let error as NSError {
// Then
XCTAssertEqual(error.domain, "Decoder Test Error", "Error domain did not match expected value.")
XCTAssertEqual(error.code, 42, "Error code did not match expected value.")
} catch {
XCTFail("Parser error was of incorrect type.")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should switch over to using the XCTAssertThrowsError APIs here.

}
}
}
6 changes: 6 additions & 0 deletions Tests/Decoders.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,9 @@ struct TestObjectDecoder: Decoder {
)
}
}

struct ErrorThrowingDecoder: Decoder {
func decode(_ object: Any) throws -> Any {
throw NSError(domain: "Decoder Test Error", code: 42, userInfo: nil)
}
}
12 changes: 12 additions & 0 deletions Tests/ModelObjects.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,15 @@ extension InvalidDecodable: Decodable {
invalid = entity[invalidKeyPath] as! String
}
}

// MARK: -

struct ErrorThrowingDecodable {}

// MARK: -

extension ErrorThrowingDecodable: Decodable {
init(json: Any) throws {
throw NSError(domain: "Decodable Test Error", code: 42, userInfo: nil)
}
}