Network error handling fix

This commit is contained in:
Anton Popkov 2018-10-26 15:20:47 +03:00
parent ad8fe894bf
commit 652c55a936
4 changed files with 41 additions and 111 deletions

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

@ -60,27 +60,6 @@ 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 {
let response = self.base.delegate.data ?? Data()
switch $0 {
case let urlError as URLError:
switch urlError.code {
case .notConnectedToInternet:
throw RequestError.noConnection
default:
throw RequestError.network(error: urlError, response: response)
}
case let afError as AFError:
switch afError {
case .responseSerializationFailed, .responseValidationFailed:
throw RequestError.invalidResponse(error: afError, response: response)
default:
throw RequestError.network(error: afError, response: response)
}
default:
throw RequestError.network(error: $0, response: response)
}
}
}
}

View File

@ -51,7 +51,10 @@ 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)
.catchErrorAndWrap(for: $0)
}
}
/// Method that executes request and serializes response into target object
@ -64,7 +67,42 @@ 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)
.catchErrorAndWrap(for: $0)
}
}
}
private extension ObservableType {
func catchErrorAndWrap(for request: DataRequest) -> Observable<E> {
return catchError {
let resultError: RequestError
let response = request.delegate.data ?? Data()
switch $0 {
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: $0, response: response)
}
throw resultError
}
}
}