Error handling basic support
This commit is contained in:
parent
69df75a94a
commit
2ff0057f12
|
|
@ -29,6 +29,7 @@
|
|||
EF05EDFC1EB0D77400CAE7B6 /* DefaultNetworkService.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF05EDFA1EB0D77400CAE7B6 /* DefaultNetworkService.swift */; };
|
||||
EF05EDFD1EB0D77400CAE7B6 /* ApiNetworkService.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF05EDFB1EB0D77400CAE7B6 /* ApiNetworkService.swift */; };
|
||||
EF05EE021EB206C000CAE7B6 /* LoadingBarButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF05EE011EB206C000CAE7B6 /* LoadingBarButton.swift */; };
|
||||
EF05EE041EB21A2D00CAE7B6 /* ApiErrorProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = EF05EE031EB21A2D00CAE7B6 /* ApiErrorProtocol.swift */; };
|
||||
/* End PBXBuildFile section */
|
||||
|
||||
/* Begin PBXFileReference section */
|
||||
|
|
@ -58,6 +59,7 @@
|
|||
EF05EDFA1EB0D77400CAE7B6 /* DefaultNetworkService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DefaultNetworkService.swift; sourceTree = "<group>"; };
|
||||
EF05EDFB1EB0D77400CAE7B6 /* ApiNetworkService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ApiNetworkService.swift; sourceTree = "<group>"; };
|
||||
EF05EE011EB206C000CAE7B6 /* LoadingBarButton.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LoadingBarButton.swift; sourceTree = "<group>"; };
|
||||
EF05EE031EB21A2D00CAE7B6 /* ApiErrorProtocol.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ApiErrorProtocol.swift; sourceTree = "<group>"; };
|
||||
/* End PBXFileReference section */
|
||||
|
||||
/* Begin PBXFrameworksBuildPhase section */
|
||||
|
|
@ -147,6 +149,7 @@
|
|||
isa = PBXGroup;
|
||||
children = (
|
||||
EF05EDB91EAF705500CAE7B6 /* ApiError.swift */,
|
||||
EF05EE031EB21A2D00CAE7B6 /* ApiErrorProtocol.swift */,
|
||||
EF05EDBA1EAF705500CAE7B6 /* ConnectionError.swift */,
|
||||
);
|
||||
path = Enums;
|
||||
|
|
@ -361,6 +364,7 @@
|
|||
isa = PBXSourcesBuildPhase;
|
||||
buildActionMask = 2147483647;
|
||||
files = (
|
||||
EF05EE041EB21A2D00CAE7B6 /* ApiErrorProtocol.swift in Sources */,
|
||||
EF05EDB81EAF704800CAE7B6 /* UserDefaults+UserService.swift in Sources */,
|
||||
EF05EDE11EAFA74200CAE7B6 /* BasePassCodeViewController.swift in Sources */,
|
||||
EF05EDC61EAF70EB00CAE7B6 /* TouchIDService.swift in Sources */,
|
||||
|
|
|
|||
|
|
@ -0,0 +1,23 @@
|
|||
//
|
||||
// ApiErrorProtocol.swift
|
||||
// LeadKitAdditions
|
||||
//
|
||||
// Created by Alexey Gerasimov on 27/04/2017.
|
||||
// Copyright © 2017 TouchInstinct. All rights reserved.
|
||||
//
|
||||
|
||||
public protocol ApiErrorProtocol: RawRepresentable {}
|
||||
|
||||
extension Error {
|
||||
|
||||
public func isApiError<T: ApiErrorProtocol>(_ apiErrorType: T) -> Bool where T.RawValue == Int {
|
||||
if let error = self as? ApiError,
|
||||
case let .error(code: code, message: _) = error,
|
||||
code == apiErrorType.rawValue {
|
||||
return true
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -66,4 +66,27 @@ public extension Observable {
|
|||
}
|
||||
}
|
||||
|
||||
public func handleApiError<T: ApiErrorProtocol>(_ apiErrorType: T,
|
||||
handler: @escaping () -> Void) -> Observable<Observable.E>
|
||||
where T.RawValue == Int {
|
||||
|
||||
return observeOn(CurrentThreadScheduler.instance)
|
||||
.do(onError: { error in
|
||||
if error.isApiError(apiErrorType) {
|
||||
handler()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
public func changeLoadingBehaviour(isLoading: PublishSubject<Bool>) -> Observable<Observable.E> {
|
||||
return observeOn(CurrentThreadScheduler.instance)
|
||||
.do(onNext: { _ in
|
||||
isLoading.onNext(false)
|
||||
}, onError: { _ in
|
||||
isLoading.onNext(false)
|
||||
}, onSubscribe: { _ in
|
||||
isLoading.onNext(true)
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -64,21 +64,4 @@ open class DefaultNetworkService: NetworkService {
|
|||
return sessionManager
|
||||
}
|
||||
|
||||
public static func apiRequestParameters(url: String, parameters: [String: Any] = [:]) -> ApiRequestParameters {
|
||||
return ApiRequestParameters(baseUrl: baseUrl, url: url, parameters: parameters)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private extension ApiRequestParameters {
|
||||
|
||||
init(baseUrl: String, url: String, parameters: [String: Any] = [:]) {
|
||||
|
||||
self.init(url: baseUrl + url,
|
||||
method: .post,
|
||||
parameters: parameters,
|
||||
encoding: JSONEncoding.default,
|
||||
headers: nil)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue