Skip to content

Commit

Permalink
Merge pull request #257 from Kinvey/feature/MLIBZ-2246-entity_not_fou…
Browse files Browse the repository at this point in the history
…nd_error

MLIBZ-2246: find by id must return entity not found error
  • Loading branch information
heyzooi committed Dec 13, 2017
2 parents af403a4 + 7e63c16 commit 6d9746c
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 14 deletions.
31 changes: 21 additions & 10 deletions Kinvey/Kinvey/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,21 @@ import Foundation
/// Enum that contains all error types in the library.
public enum Error: Swift.Error, LocalizedError, CustomStringConvertible, CustomDebugStringConvertible {

/// Constant for 401 responses where the credentials are not enough to complete the request.
public static let InsufficientCredentials = "InsufficientCredentials"

/// Constant for 401 responses where the credentials are not valid to complete the request.
public static let InvalidCredentials = "InvalidCredentials"

/// Constant for 400 response where the number of results exceeded the limit.
public static let ResultSetSizeExceeded = "ResultSetSizeExceeded"
public enum Keys: String {

/// Constant for 401 responses where the credentials are not enough to complete the request.
case insufficientCredentials = "InsufficientCredentials"

/// Constant for 401 responses where the credentials are not valid to complete the request.
case invalidCredentials = "InvalidCredentials"

/// Constant for 400 response where the number of results exceeded the limit.
case resultSetSizeExceeded = "ResultSetSizeExceeded"

/// Constant for 404 response where the entity was not found in the collection
case entityNotFound = "EntityNotFound"

}

/// Error where Object ID is required.
case objectIdMissing
Expand Down Expand Up @@ -74,6 +81,8 @@ public enum Error: Swift.Error, LocalizedError, CustomStringConvertible, CustomD
/// Error when the number of results exceeded the limit
case resultSetSizeExceeded(debug: String, description: String)

case entityNotFound(debug: String, description: String)

/// Error localized description.
public var description: String {
let bundle = Bundle(for: Client.self)
Expand All @@ -86,7 +95,8 @@ public enum Error: Swift.Error, LocalizedError, CustomStringConvertible, CustomD
.missingConfiguration(_, _, _, let description),
.appNotFound(let description),
.forbidden(let description),
.resultSetSizeExceeded(_, let description):
.resultSetSizeExceeded(_, let description),
.entityNotFound(_, let description):
return description
case .objectIdMissing:
return NSLocalizedString("Error.objectIdMissing", bundle: bundle, comment: "")
Expand Down Expand Up @@ -123,7 +133,8 @@ public enum Error: Swift.Error, LocalizedError, CustomStringConvertible, CustomD
.dataLinkEntityNotFound(_, _, let debug, _),
.missingConfiguration(_, _, let debug, _),
.unauthorized(_, _, _, let debug, _),
.resultSetSizeExceeded(let debug, _):
.resultSetSizeExceeded(let debug, _),
.entityNotFound(let debug, _):
return debug
default:
return description
Expand Down
10 changes: 9 additions & 1 deletion Kinvey/Kinvey/Kinvey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,19 @@ func buildError(
} else if let response = response,
response.isBadRequest,
let json = json,
json["error"] == Error.ResultSetSizeExceeded,
json["error"] == Error.Keys.resultSetSizeExceeded.rawValue,
let debug = json["debug"],
let description = json["description"]
{
return Error.resultSetSizeExceeded(debug: debug, description: description)
} else if let response = response,
response.isNotFound,
let json = json,
json["error"] == Error.Keys.entityNotFound.rawValue,
let debug = json["debug"],
let description = json["description"]
{
return Error.entityNotFound(debug: debug, description: description)
} else if let response = response,
let json = client.responseParser.parse(data)
{
Expand Down
2 changes: 1 addition & 1 deletion Kinvey/Kinvey/PushOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ internal class PushOperation<T: Persistable>: SyncOperation<T, UInt, [Swift.Erro
let debug = json["debug"],
let description = json["description"]
{
if error == Error.InsufficientCredentials {
if error == Error.Keys.insufficientCredentials.rawValue {
self.sync?.removePendingOperation(pendingOperation)
}
let error = Error.unauthorized(
Expand Down
4 changes: 2 additions & 2 deletions Kinvey/KinveyTests/AclTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class AclTestCase: StoreTestCase {
}
switch error {
case .unauthorized(_, _, let error, _, _):
XCTAssertEqual(error, Kinvey.Error.InsufficientCredentials)
XCTAssertEqual(error, Kinvey.Error.Keys.insufficientCredentials.rawValue)
default:
XCTFail()
}
Expand Down Expand Up @@ -166,7 +166,7 @@ class AclTestCase: StoreTestCase {
if let error = errors.first as? Kinvey.Error {
switch error {
case .unauthorized(_, _, let error, _, _):
XCTAssertEqual(error, Kinvey.Error.InsufficientCredentials)
XCTAssertEqual(error, Kinvey.Error.Keys.insufficientCredentials.rawValue)
default:
XCTFail()
}
Expand Down
41 changes: 41 additions & 0 deletions Kinvey/KinveyTests/NetworkStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -712,6 +712,47 @@ class NetworkStoreTests: StoreTestCase {
}
}

func testFindByIdEntityNotFoundError() {
mockResponse(
statusCode: 404,
json: [
"error" : "EntityNotFound",
"description" : "This entity not found in the collection",
"debug" : ""
]
)
defer {
setURLProtocol(nil)
}

weak var expectationFind = expectation(description: "Find")

store.find("id-not-found", options: Options(readPolicy: .forceNetwork)) {
switch $0 {
case .success:
XCTFail()
case .failure(let error):
XCTAssertTrue(error is Kinvey.Error)

if let error = error as? Kinvey.Error {
switch error {
case .entityNotFound(let debug, let description):
XCTAssertEqual(debug, "")
XCTAssertEqual(description, "This entity not found in the collection")
default:
XCTFail()
}
}
}

expectationFind?.fulfill()
}

waitForExpectations(timeout: defaultTimeout) { error in
expectationFind = nil
}
}

func testFindMethodObjectIdMissingAndRandomSampleValidationStrategy() {
mockResponse(json: [
[
Expand Down

0 comments on commit 6d9746c

Please sign in to comment.