From 283444df55ae3841a52139abd805d7cb20d946a4 Mon Sep 17 00:00:00 2001 From: Syo Ikeda Date: Tue, 27 Dec 2016 23:52:26 +0900 Subject: [PATCH] Utilize AnyError --- ReactiveObjCBridge/ObjectiveCBridging.swift | 12 ++++++------ ReactiveObjCBridgeTests/ObjectiveCBridgingSpec.swift | 6 +++--- ReactiveObjCBridgeTests/TestError.swift | 7 +++++++ 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/ReactiveObjCBridge/ObjectiveCBridging.swift b/ReactiveObjCBridge/ObjectiveCBridging.swift index 192ea534b..6e09e1697 100644 --- a/ReactiveObjCBridge/ObjectiveCBridging.swift +++ b/ReactiveObjCBridge/ObjectiveCBridging.swift @@ -119,14 +119,14 @@ private func defaultNSError(_ message: String, file: String, line: Int) -> NSErr /// - line: Current line in file. /// /// - returns: Signal producer created from the provided signal. -public func bridgedSignalProducer(from signal: RACSignal, file: String = #file, line: Int = #line) -> SignalProducer { - return SignalProducer { observer, disposable in +public func bridgedSignalProducer(from signal: RACSignal, file: String = #file, line: Int = #line) -> SignalProducer { + return SignalProducer { observer, disposable in let next: (_ value: Value?) -> Void = { obj in observer.send(value: obj) } - let failed: (_ nsError: Swift.Error?) -> () = { - observer.send(error: ($0 as? NSError) ?? defaultNSError("Nil RACSignal error", file: file, line: line)) + let failed: (_ error: Swift.Error?) -> () = { error in + observer.send(error: AnyError(error ?? defaultNSError("Nil RACSignal error", file: file, line: line))) } let completed = { @@ -279,14 +279,14 @@ extension ActionProtocol { /// - line: Current line in file. /// /// - returns: Action created from `self`. -public func bridgedAction(from command: RACCommand, file: String = #file, line: Int = #line) -> Action { +public func bridgedAction(from command: RACCommand, file: String = #file, line: Int = #line) -> Action { let enabledProperty = MutableProperty(true) enabledProperty <~ bridgedSignalProducer(from: command.enabled) .map { $0 as! Bool } .flatMapError { _ in SignalProducer(value: false) } - return Action(enabledIf: enabledProperty) { input -> SignalProducer in + return Action(enabledIf: enabledProperty) { input -> SignalProducer in let signal: RACSignal = command.execute(input) return bridgedSignalProducer(from: signal) diff --git a/ReactiveObjCBridgeTests/ObjectiveCBridgingSpec.swift b/ReactiveObjCBridgeTests/ObjectiveCBridgingSpec.swift index fdcf44cb2..22c712a3f 100644 --- a/ReactiveObjCBridgeTests/ObjectiveCBridgingSpec.swift +++ b/ReactiveObjCBridgeTests/ObjectiveCBridgingSpec.swift @@ -78,13 +78,13 @@ class ObjectiveCBridgingSpec: QuickSpec { } it("should forward errors") { - let error = TestError.default as NSError + let error = TestError.default let racSignal = RACSignal.error(error) let producer = bridgedSignalProducer(from: racSignal) let result = producer.last() - expect(result?.error) == error + expect(result?.error) == AnyError(error) } } @@ -214,7 +214,7 @@ class ObjectiveCBridgingSpec: QuickSpec { var enabledSubject: RACSubject! var enabled = false - var action: Action! + var action: Action! beforeEach { enabledSubject = RACSubject() diff --git a/ReactiveObjCBridgeTests/TestError.swift b/ReactiveObjCBridgeTests/TestError.swift index b9d59df8a..fd42d1d02 100644 --- a/ReactiveObjCBridgeTests/TestError.swift +++ b/ReactiveObjCBridgeTests/TestError.swift @@ -18,6 +18,13 @@ internal enum TestError: Int { extension TestError: Error { } +extension AnyError: Equatable { + public static func ==(lhs: AnyError, rhs: AnyError) -> Bool { + return lhs.error._code == rhs.error._code + && lhs.error._domain == rhs.error._domain + } +} + internal extension SignalProducerProtocol { /// Halts if an error is emitted in the receiver signal.