13

Can I raise an error from a map function or do I need to use flatMap? The error should be reported to onError. In RxJava I can simply throw an exception. How can I do the following (pseudo-)code

observable.map( value -> {if (value.isIllegal) raiseError else return value.count})

Obviously it is possible using a flatMap, but I am looking for a map solution.

christopher.online
  • 2,614
  • 3
  • 28
  • 52
Benjamin Mesing
  • 4,075
  • 1
  • 18
  • 22

1 Answers1

27

Works the same way in RxSwift -

        enum MyError: Error {
            case myError
        }

        Observable
            .from([1,2,3])
            .map { (element) -> Int in
                throw MyError.myError
            }
            .subscribe { event in
                os_log("%@", "\(event)")
            }
            .disposed(by: self.disposeBag)
Maxim Volgin
  • 3,957
  • 1
  • 23
  • 38