Merge pull request #171 from TouchInstinct/feature/request_error_response

Feature/request error response
This commit is contained in:
Ivan Babkin 2018-10-26 18:01:42 +03:00 committed by GitHub
commit 1a5e37f527
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 52 additions and 116 deletions

View File

@ -1,5 +1,9 @@
# Changelog
### 0.9.2
- **Update**: Add response to `RequestError`.
- **Fix**: Update `SessionManager+Extensions` to catch network connection error.
### 0.9.1
- **Update**: `DataRequest+Extensions` time out as network error

View File

@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "LeadKit"
s.version = "0.9.1"
s.version = "0.9.2"
s.summary = "iOS framework with a bunch of tools for rapid development"
s.homepage = "https://github.com/TouchInstinct/LeadKit"
s.license = "Apache License, Version 2.0"

View File

@ -2397,7 +2397,6 @@
67186B2C1EB248F100CFAFFB /* Sources */,
67186B2D1EB248F100CFAFFB /* Frameworks */,
67186B2E1EB248F100CFAFFB /* Resources */,
B285084CE10354367156E122 /* [CP] Embed Pods Frameworks */,
);
buildRules = (
);
@ -2459,7 +2458,6 @@
6782BBA41EB31D5A0086E0B8 /* Sources */,
6782BBA51EB31D5A0086E0B8 /* Frameworks */,
6782BBA61EB31D5A0086E0B8 /* Resources */,
C7322258346BDF6E53D19895 /* [CP] Embed Pods Frameworks */,
);
buildRules = (
);
@ -2500,7 +2498,6 @@
67952DC91EB327B400B3BA1A /* Sources */,
67952DCA1EB327B400B3BA1A /* Frameworks */,
67952DCB1EB327B400B3BA1A /* Resources */,
FD672381E62A00830715CF1A /* [CP] Embed Pods Frameworks */,
);
buildRules = (
);
@ -2865,36 +2862,6 @@
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n";
showEnvVarsInLog = 0;
};
B285084CE10354367156E122 /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
"${SRCROOT}/Pods/Target Support Files/Pods-LeadKit iOSTests/Pods-LeadKit iOSTests-frameworks.sh",
"${BUILT_PRODUCTS_DIR}/Alamofire-iOS/Alamofire.framework",
"${BUILT_PRODUCTS_DIR}/RxAlamofire-iOS/RxAlamofire.framework",
"${BUILT_PRODUCTS_DIR}/RxCocoa-iOS/RxCocoa.framework",
"${BUILT_PRODUCTS_DIR}/RxSwift-iOS/RxSwift.framework",
"${BUILT_PRODUCTS_DIR}/SwiftDate-iOS/SwiftDate.framework",
"${BUILT_PRODUCTS_DIR}/TableKit/TableKit.framework",
"${BUILT_PRODUCTS_DIR}/UIScrollView-InfiniteScroll/UIScrollView_InfiniteScroll.framework",
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Alamofire.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RxAlamofire.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RxCocoa.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RxSwift.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SwiftDate.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/TableKit.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/UIScrollView_InfiniteScroll.framework",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-LeadKit iOSTests/Pods-LeadKit iOSTests-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
BCF262D3C2C3337546F84061 /* [CP] Check Pods Manifest.lock */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
@ -2913,60 +2880,6 @@
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n# This output is used by Xcode 'outputs' to avoid re-running this script phase.\necho \"SUCCESS\" > \"${SCRIPT_OUTPUT_FILE_0}\"\n";
showEnvVarsInLog = 0;
};
C7322258346BDF6E53D19895 /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
"${SRCROOT}/Pods/Target Support Files/Pods-LeadKit tvOSTests/Pods-LeadKit tvOSTests-frameworks.sh",
"${BUILT_PRODUCTS_DIR}/Alamofire-tvOS/Alamofire.framework",
"${BUILT_PRODUCTS_DIR}/RxAlamofire-tvOS/RxAlamofire.framework",
"${BUILT_PRODUCTS_DIR}/RxCocoa-tvOS/RxCocoa.framework",
"${BUILT_PRODUCTS_DIR}/RxSwift-tvOS/RxSwift.framework",
"${BUILT_PRODUCTS_DIR}/SwiftDate-tvOS/SwiftDate.framework",
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Alamofire.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RxAlamofire.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RxCocoa.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RxSwift.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SwiftDate.framework",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-LeadKit tvOSTests/Pods-LeadKit tvOSTests-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
FD672381E62A00830715CF1A /* [CP] Embed Pods Frameworks */ = {
isa = PBXShellScriptBuildPhase;
buildActionMask = 2147483647;
files = (
);
inputPaths = (
"${SRCROOT}/Pods/Target Support Files/Pods-LeadKit iOS ExtensionsTests/Pods-LeadKit iOS ExtensionsTests-frameworks.sh",
"${BUILT_PRODUCTS_DIR}/Alamofire-iOS/Alamofire.framework",
"${BUILT_PRODUCTS_DIR}/RxAlamofire-iOS/RxAlamofire.framework",
"${BUILT_PRODUCTS_DIR}/RxCocoa-iOS/RxCocoa.framework",
"${BUILT_PRODUCTS_DIR}/RxSwift-iOS/RxSwift.framework",
"${BUILT_PRODUCTS_DIR}/SwiftDate-iOS/SwiftDate.framework",
"${BUILT_PRODUCTS_DIR}/TableKit/TableKit.framework",
);
name = "[CP] Embed Pods Frameworks";
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/Alamofire.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RxAlamofire.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RxCocoa.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/RxSwift.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/SwiftDate.framework",
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/TableKit.framework",
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "\"${SRCROOT}/Pods/Target Support Files/Pods-LeadKit iOS ExtensionsTests/Pods-LeadKit iOS ExtensionsTests-frameworks.sh\"\n";
showEnvVarsInLog = 0;
};
/* End PBXShellScriptBuildPhase section */
/* Begin PBXSourcesBuildPhase section */

View File

@ -45,4 +45,4 @@ SPEC CHECKSUMS:
PODFILE CHECKSUM: fd9789ede36fcba83a5b12f15edf8b32183698ba
COCOAPODS: 1.5.3
COCOAPODS: 1.6.0.beta.2

View File

@ -32,8 +32,8 @@ import Alamofire
public enum RequestError: Error {
case noConnection
case network(error: Error)
case invalidResponse(error: AFError)
case mapping(error: Error, response: Any)
case network(error: Error, response: Data?)
case invalidResponse(error: AFError, response: Data?)
case mapping(error: Error, response: Data)
}

View File

@ -58,28 +58,7 @@ public extension Reactive where Base: DataRequest {
}
private func response(onQueue queue: DispatchQueue) -> Observable<(HTTPURLResponse, Data)> {
return responseData()
.observeOn(SerialDispatchQueueScheduler(queue: queue, internalSerialQueueName: queue.label))
.catchError {
switch $0 {
case let urlError as URLError:
switch urlError.code {
case .notConnectedToInternet:
throw RequestError.noConnection
default:
throw RequestError.network(error: urlError)
}
case let afError as AFError:
switch afError {
case .responseSerializationFailed, .responseValidationFailed:
throw RequestError.invalidResponse(error: afError)
default:
throw RequestError.network(error: afError)
}
default:
throw RequestError.network(error: $0)
}
}
return responseResult(queue: queue, responseSerializer: DataRequest.dataResponseSerializer())
}
}

View File

@ -51,7 +51,11 @@ public extension Reactive where Base: SessionManager {
-> Observable<(response: HTTPURLResponse, model: T)> {
return apiRequest(requestParameters: requestParameters)
.flatMap { $0.rx.apiResponse(mappingQueue: self.base.mappingQueue, decoder: decoder) }
.flatMap {
$0.rx.apiResponse(mappingQueue: self.base.mappingQueue, decoder: decoder)
.catchAsRequestError(with: $0)
}
.catchAsRequestError()
}
/// Method that executes request and serializes response into target object
@ -64,7 +68,43 @@ public extension Reactive where Base: SessionManager {
-> Observable<(response: HTTPURLResponse, model: T)> {
return apiRequest(requestParameters: requestParameters)
.flatMap { $0.rx.observableApiResponse(mappingQueue: self.base.mappingQueue, decoder: decoder) }
.flatMap {
$0.rx.observableApiResponse(mappingQueue: self.base.mappingQueue, decoder: decoder)
.catchAsRequestError(with: $0)
}
.catchAsRequestError()
}
}
private extension ObservableType {
func catchAsRequestError(with request: DataRequest? = nil) -> Observable<E> {
return catchError { error in
let resultError: RequestError
let response = request?.delegate.data
switch error {
case let requestError as RequestError:
resultError = requestError
case let urlError as URLError:
switch urlError.code {
case .notConnectedToInternet:
resultError = .noConnection
default:
resultError = .network(error: urlError, response: response)
}
case let afError as AFError:
switch afError {
case .responseSerializationFailed, .responseValidationFailed:
resultError = .invalidResponse(error: afError, response: response)
default:
resultError = .network(error: afError, response: response)
}
default:
resultError = .network(error: error, response: response)
}
throw resultError
}
}
}