Skip to content

Commit

Permalink
Added statusCode in error/failure closures
Browse files Browse the repository at this point in the history
  • Loading branch information
MrCloud committed May 30, 2018
1 parent ee6e816 commit 4da5c9a
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@ class UserInteractor: UserInteractorInput {
print(error.localizedDescription)
}
}

func getAuthUser(completion: @escaping (User) -> Void) {
_ = UserService().fetchAuthUser(success: { (user) in
completion(user)
}, error: { (error) in
print(error.localizedDescription)
})
}




Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,5 @@ protocol UserInteractorInput {
func getUser(username: String)//, completion: @escaping (User) -> Void)
func getUserRepos(username: String, completion: @escaping ([Repo]) -> Void)
func getEmojis(completion: @escaping (Any) -> Void)
func getAuthUser(completion: @escaping (User) -> Void)
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class UserPresenter: UserModuleInput, UserViewOutput, UserInteractorOutput {
interactor.getEmojis { (json) in
print(json)
}
interactor.getAuthUser { (user) in
print(user)
}
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,30 @@ class UserService: RPServiceClient<UserServiceAPI> {
}
}

func fetchEmojis(success: @escaping (Any) -> Void, error: (Swift.Error) -> Void) -> Cancellable {
func fetchEmojis(success: @escaping (Any) -> Void, error: (RPServiceClientError) -> Void) -> Cancellable {

let target = UserServiceAPI.getEmojis()
return super.requestJSON(target: target, success: success) { (error) in
print(error)
}
}

func fetchAuthUser(success: @escaping (User) -> Void, error: (RPServiceClientError) -> Void) -> Cancellable {

let target = UserServiceAPI.getAuthUser()
return super.requestObject(target: target, success: success) { (error) in
switch error {
case .RequestError(let statusCode, let json):
print("Error occured with statusCode: \(statusCode), error: \(json)")
case .JSONParsing(let cause):
print(cause)
case .InvalidMapping(let json):
print(json)
case .RequestFailure(_, let cause):
print("cause: \(cause)")
}
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ enum UserServiceAPI {
case getUserInfos(username: String)
case getUserRepos(username: String)
case getEmojis()
case getAuthUser()
}

// MARK: - TargetType Protocol Implementation
Expand All @@ -44,48 +45,50 @@ extension UserServiceAPI : TargetType {
return "/users/\(username)/repos"
case .getEmojis():
return "/emojis"
case .getAuthUser():
return "/user"
}
}

var method: Moya.Method {
switch self {
case .getUserInfos, .getUserRepos, .getEmojis:
case .getUserInfos, .getUserRepos, .getEmojis, .getAuthUser():
return .get
}
}

var parameters: [String: Any]? {
switch self {
case .getUserInfos, .getUserRepos, .getEmojis:
case .getUserInfos, .getUserRepos, .getEmojis, .getAuthUser():
return nil
}
}

var parameterEncoding: ParameterEncoding {
switch self {
case .getUserInfos, .getUserRepos, .getEmojis:
case .getUserInfos, .getUserRepos, .getEmojis, .getAuthUser():
return URLEncoding()
}
}

var sampleData: Data {
switch self {
case .getUserInfos, .getUserRepos, .getEmojis:
case .getUserInfos, .getUserRepos, .getEmojis, .getAuthUser():
return "".data(using: .utf8)!
}
}

var task: Task {
switch self {
case .getUserInfos, .getUserRepos, .getEmojis:
case .getUserInfos, .getUserRepos, .getEmojis, .getAuthUser():
return .request
}
}


var multipartBody: [MultipartFormData]? {
switch self {
case .getUserInfos, .getUserRepos, .getEmojis:
case .getUserInfos, .getUserRepos, .getEmojis, .getAuthUser():
return nil
}
}
Expand Down
6 changes: 3 additions & 3 deletions RPHTTPServiceClient/Classes/RPServiceClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -141,12 +141,12 @@ open class RPServiceClient<Target> where Target : TargetType {
case let .success(response): // Success with status >= 400
do {
let json = try response.mapJSON()
failure(RPServiceClientError.RequestError(json: json))
failure(RPServiceClientError.RequestError(statusCode: response.statusCode, json: json))
} catch (let error) {
failure(RPServiceClientError.JSONParsing(cause: error))
}
case let .failure(error):
failure(RPServiceClientError.RequestFailure(cause: error))
failure(RPServiceClientError.RequestFailure(statusCode: error.response?.statusCode, cause: error))
}
})
return cancellable
Expand Down Expand Up @@ -177,7 +177,7 @@ open class RPServiceClient<Target> where Target : TargetType {
NSLocalizedDescriptionKey: errorMsg
]
let error = NSError(domain: "RPServiceClientErrorDomain", code: 1001, userInfo: userInfo)
let bufferError = RPServiceClientError.RequestFailure(cause: error)
let bufferError = RPServiceClientError.RequestFailure(statusCode: errorResult.code, cause: error)
return bufferError
}
}
6 changes: 4 additions & 2 deletions RPHTTPServiceClient/Classes/RPServiceClientError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ public enum RPServiceClientError: Swift.Error {

/**
Request error, occurring when the API is returning a statusCode of 4xx
- parameter cause: HTTP response status code
- parameter json: the json wrapped error
*/
case RequestError(json: Any)
case RequestError(statusCode: Int, json: Any)

/**
Request failure, occurring when the API is returning an error
- parameter cause: HTTP response status code, if any
- parameter cause: cause of the error
*/
case RequestFailure(cause: Swift.Error)
case RequestFailure(statusCode: Int?, cause: Swift.Error)
}

0 comments on commit 4da5c9a

Please sign in to comment.