From 536128f8e8a6ff46b35f7a56aae36b3b3a5c01c2 Mon Sep 17 00:00:00 2001 From: Krunoslav Zaher Date: Sat, 19 Dec 2015 14:38:41 +0100 Subject: [PATCH] Adds error handling during `bindNext`. --- RxCocoa/Common/Observable+Bind.swift | 28 ++++++++++++++++++++++------ RxCocoa/Common/RxCocoa.swift | 9 --------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/RxCocoa/Common/Observable+Bind.swift b/RxCocoa/Common/Observable+Bind.swift index a7188c33..4dc99ad7 100644 --- a/RxCocoa/Common/Observable+Bind.swift +++ b/RxCocoa/Common/Observable+Bind.swift @@ -16,7 +16,8 @@ extension ObservableType { /** Creates new subscription and sends elements to observer. - In this form it's equivalent to `subscribe` method, but it communicates intent better. + In this form it's equivalent to `subscribe` method, but it communicates intent better, and enables + writing more consistent binding code. - parameter observer: Observer that receives events. - returns: Disposable object that can be used to unsubscribe the observer. @@ -29,8 +30,8 @@ extension ObservableType { /** Creates new subscription and sends elements to variable. - In release mode error will be logged in case attempting to bind an error to variable. - In debug mode exception will be thrown in case attempting to bind error to variable. + In case error occurs in debug mode, `fatalError` will be raised. + In case error occurs in release mode, `error` will be logged. - parameter variable: Target variable for sequence elements. - returns: Disposable object that can be used to unsubscribe the observer. @@ -42,7 +43,12 @@ extension ObservableType { case let .Next(element): variable.value = element case let .Error(error): - bindingErrorToVariable(error) + let error = "Binding error to variable: \(error)" + #if DEBUG + rxFatalError(error) + #else + print(error) + #endif case .Completed: break } @@ -79,13 +85,23 @@ extension ObservableType { /** - Subscribes an element handler to an observable sequence. + Subscribes an element handler to an observable sequence. + + In case error occurs in debug mode, `fatalError` will be raised. + In case error occurs in release mode, `error` will be logged. - parameter onNext: Action to invoke for each element in the observable sequence. - returns: Subscription object used to unsubscribe from the observable sequence. */ @warn_unused_result(message="http://git.io/rxs.ud") public func bindNext(onNext: E -> Void) -> Disposable { - return subscribeNext(onNext) + return subscribe(onNext: onNext, onError: { error in + let error = "Binding error to variable: \(error)" + #if DEBUG + rxFatalError(error) + #else + print(error) + #endif + }) } } \ No newline at end of file diff --git a/RxCocoa/Common/RxCocoa.swift b/RxCocoa/Common/RxCocoa.swift index 3207c33c..b1070990 100644 --- a/RxCocoa/Common/RxCocoa.swift +++ b/RxCocoa/Common/RxCocoa.swift @@ -228,15 +228,6 @@ func bindingErrorToInterface(error: ErrorType) { #endif } -func bindingErrorToVariable(error: ErrorType) { - let error = "Binding error to variable: \(error)" -#if DEBUG - rxFatalError(error) -#else - print(error) -#endif -} - // MARK: Abstract methods @noreturn func rxAbstractMethodWithMessage(message: String) {