Skip to content

Commit

Permalink
fix: some async/await methods won't throw errors (#334)
Browse files Browse the repository at this point in the history
* refactor: remove force unwrapping

* fix async void returns

* add change log

* improve codecov

* Update .codecov.yml
  • Loading branch information
cbaker6 committed Jan 26, 2022
1 parent d2de796 commit 879055b
Show file tree
Hide file tree
Showing 17 changed files with 328 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ coverage:
status:
patch:
default:
target: auto
target: 78
changes: false
project:
default:
Expand Down
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### main

[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/3.1.2...main)
[Full Changelog](https://github.com/parse-community/Parse-Swift/compare/4.0.0...main)
* _Contributing to this repo? Add info about your change here to be included in the next release_

### 4.0.0
Expand Down Expand Up @@ -32,6 +32,7 @@ __Improvements__
- (Breaking Change) Change the following method parameter names: isUsingTransactions -> usingTransactions, isAllowingCustomObjectIds -> allowingCustomObjectIds, isUsingEqualQueryConstraint -> usingEqualQueryConstraint, isMigratingFromObjcSDK -> migratingFromObjcSDK, isDeletingKeychainIfNeeded -> deletingKeychainIfNeeded ([#323](https://github.com/parse-community/Parse-Swift/pull/323)), thanks to [Corey Baker](https://github.com/cbaker6).

__Fixes__
- Async/await methods that return void would no throw errors received from server ([#334](https://github.com/parse-community/Parse-Swift/pull/334)), thanks to [Corey Baker](https://github.com/cbaker6).
- Always check for ParseError first when decoding responses from the server. Before this fix, this could cause issues depending on how calls are made from the Swift SDK ([#332](https://github.com/parse-community/Parse-Swift/pull/332)), thanks to [Corey Baker](https://github.com/cbaker6).

### 3.1.2
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ import PackageDescription
let package = Package(
name: "YOUR_PROJECT_NAME",
dependencies: [
.package(url: "https://github.com/parse-community/Parse-Swift", from: "3.1.2"),
.package(url: "https://github.com/parse-community/Parse-Swift", .upToNextMajor(from: "4.0.0")),
]
)
```
Expand Down
5 changes: 4 additions & 1 deletion Sources/ParseSwift/Objects/ParseInstallation+async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,12 @@ public extension ParseInstallation {
- important: If an object deleted has the same objectId as current, it will automatically update the current.
*/
func delete(options: API.Options = []) async throws {
_ = try await withCheckedThrowingContinuation { continuation in
let result = try await withCheckedThrowingContinuation { continuation in
self.delete(options: options, completion: continuation.resume)
}
if case let .failure(error) = result {
throw error
}
}
}

Expand Down
7 changes: 4 additions & 3 deletions Sources/ParseSwift/Objects/ParseInstallation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,12 @@ extension ParseInstallation {

var foundCurrentInstallationObjects = results.filter { $0.hasSameInstallationId(as: currentInstallation) }
foundCurrentInstallationObjects = try foundCurrentInstallationObjects.sorted(by: {
if $0.updatedAt == nil || $1.updatedAt == nil {
guard let firstUpdatedAt = $0.updatedAt,
let secondUpdatedAt = $1.updatedAt else {
throw ParseError(code: .unknownError,
message: "Objects from the server should always have an 'updatedAt'")
message: "Objects from the server should always have an \"updatedAt\"")
}
return $0.updatedAt!.compare($1.updatedAt!) == .orderedDescending
return firstUpdatedAt.compare(secondUpdatedAt) == .orderedDescending
})
if let foundCurrentInstallation = foundCurrentInstallationObjects.first {
if !deleting {
Expand Down
5 changes: 4 additions & 1 deletion Sources/ParseSwift/Objects/ParseObject+async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,13 @@ public extension ParseObject {
- throws: An error of type `ParseError`.
*/
func delete(options: API.Options = []) async throws {
_ = try await withCheckedThrowingContinuation { continuation in
let result = try await withCheckedThrowingContinuation { continuation in
self.delete(options: options,
completion: continuation.resume)
}
if case let .failure(error) = result {
throw error
}
}
}

Expand Down
20 changes: 16 additions & 4 deletions Sources/ParseSwift/Objects/ParseUser+async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,12 @@ public extension ParseUser {
- throws: An error of type `ParseError`.
*/
static func logout(options: API.Options = []) async throws {
_ = try await withCheckedThrowingContinuation { continuation in
let result = try await withCheckedThrowingContinuation { continuation in
Self.logout(options: options, completion: continuation.resume)
}
if case let .failure(error) = result {
throw error
}
}

/**
Expand All @@ -115,9 +118,12 @@ public extension ParseUser {
*/
static func passwordReset(email: String,
options: API.Options = []) async throws {
_ = try await withCheckedThrowingContinuation { continuation in
let result = try await withCheckedThrowingContinuation { continuation in
Self.passwordReset(email: email, options: options, completion: continuation.resume)
}
if case let .failure(error) = result {
throw error
}
}

/**
Expand Down Expand Up @@ -147,9 +153,12 @@ public extension ParseUser {
*/
static func verificationEmail(email: String,
options: API.Options = []) async throws {
_ = try await withCheckedThrowingContinuation { continuation in
let result = try await withCheckedThrowingContinuation { continuation in
Self.verificationEmail(email: email, options: options, completion: continuation.resume)
}
if case let .failure(error) = result {
throw error
}
}

/**
Expand Down Expand Up @@ -252,9 +261,12 @@ public extension ParseUser {
- important: If an object deleted has the same objectId as current, it will automatically update the current.
*/
func delete(options: API.Options = []) async throws {
_ = try await withCheckedThrowingContinuation { continuation in
let result = try await withCheckedThrowingContinuation { continuation in
self.delete(options: options, completion: continuation.resume)
}
if case let .failure(error) = result {
throw error
}
}
}

Expand Down
25 changes: 7 additions & 18 deletions Sources/ParseSwift/Objects/ParseUser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -494,17 +494,8 @@ extension ParseUser {
completion: @escaping (Result<Self, ParseError>) -> Void) {
var options = options
options.insert(.cachePolicy(.reloadIgnoringLocalCacheData))
let username: String!
if let current = BaseParseUser.current,
let currentUsername = current.username {
username = currentUsername
} else {
username = ""
}
var method: API.Method = .POST
if !usingPost {
method = .GET
}
let username = BaseParseUser.current?.username ?? ""
let method: API.Method = usingPost ? .POST : .GET
verifyPasswordCommand(username: username,
password: password,
method: method)
Expand Down Expand Up @@ -532,10 +523,7 @@ extension ParseUser {
path: .verifyPassword,
params: params,
body: loginBody) { (data) -> Self in
var sessionToken = ""
if let currentSessionToken = BaseParseUser.current?.sessionToken {
sessionToken = currentSessionToken
}
var sessionToken = BaseParseUser.current?.sessionToken ?? ""
if let decodedSessionToken = try? ParseCoding.jsonDecoder()
.decode(LoginSignupResponse.self, from: data).sessionToken {
sessionToken = decodedSessionToken
Expand Down Expand Up @@ -826,11 +814,12 @@ extension ParseUser {

var foundCurrentUserObjects = results.filter { $0.hasSameObjectId(as: currentUser) }
foundCurrentUserObjects = try foundCurrentUserObjects.sorted(by: {
if $0.updatedAt == nil || $1.updatedAt == nil {
guard let firstUpdatedAt = $0.updatedAt,
let secondUpdatedAt = $1.updatedAt else {
throw ParseError(code: .unknownError,
message: "Objects from the server should always have an 'updatedAt'")
message: "Objects from the server should always have an \"updatedAt\"")
}
return $0.updatedAt!.compare($1.updatedAt!) == .orderedDescending
return firstUpdatedAt.compare(secondUpdatedAt) == .orderedDescending
})
if let foundCurrentUser = foundCurrentUserObjects.first {
if !deleting {
Expand Down
15 changes: 12 additions & 3 deletions Sources/ParseSwift/Types/ParseAnalytics+async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@ public extension ParseAnalytics {
static func trackAppOpened(launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil,
at date: Date? = nil,
options: API.Options = []) async throws {
_ = try await withCheckedThrowingContinuation { continuation in
let result = try await withCheckedThrowingContinuation { continuation in
Self.trackAppOpened(launchOptions: launchOptions,
at: date,
options: options,
completion: continuation.resume)
}
if case let .failure(error) = result {
throw error
}
}
#endif

Expand All @@ -58,12 +61,15 @@ public extension ParseAnalytics {
static func trackAppOpened(dimensions: [String: String]? = nil,
at date: Date? = nil,
options: API.Options = []) async throws {
_ = try await withCheckedThrowingContinuation { continuation in
let result = try await withCheckedThrowingContinuation { continuation in
Self.trackAppOpened(dimensions: dimensions,
at: date,
options: options,
completion: continuation.resume)
}
if case let .failure(error) = result {
throw error
}
}

/**
Expand All @@ -73,10 +79,13 @@ public extension ParseAnalytics {
- throws: An error of type `ParseError`.
*/
func track(options: API.Options = []) async throws {
_ = try await withCheckedThrowingContinuation { continuation in
let result = try await withCheckedThrowingContinuation { continuation in
self.track(options: options,
completion: continuation.resume)
}
if case let .failure(error) = result {
throw error
}
}

/**
Expand Down
5 changes: 4 additions & 1 deletion Sources/ParseSwift/Types/ParseFile+async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,12 @@ public extension ParseFile {
- throws: An error of type `ParseError`.
*/
func delete(options: API.Options = []) async throws {
_ = try await withCheckedThrowingContinuation { continuation in
let result = try await withCheckedThrowingContinuation { continuation in
self.delete(options: options, completion: continuation.resume)
}
if case let .failure(error) = result {
throw error
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion Tests/ParseSwiftTests/APICommandTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class APICommandTests: XCTestCase {
}
}

//This is how errors HTTP errors should typically come in
// This is how errors HTTP errors should typically come in
func testErrorHTTP400JSON() {
let parseError = ParseError(code: .connectionFailed, message: "Connection failed")
let errorKey = "error"
Expand Down
Loading

0 comments on commit 879055b

Please sign in to comment.