Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-4304

Propagate exceptions thrown in map or flatmap for Observable

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Unknown Unknown
    • 4.3.3
    • Affects Version/s: None
    • Component/s: Scala

      mongo-scala-driver: 4.3.1

      exceptions thrown in map or flatmap for Observable are not propagated up.

      take for example

      val obs: Future[Unit] =
        (for {
            _ <- collection.insert(BsonDocument())
            _ = sys.error("Boom")
          } yield ()
         ).toFuture()

      this future will never complete and lead to a timeout.

      Only exceptions thrown in an Observable are propagated up. e.g. using a `raiseError` defined as such

      def raiseError[A](e: Throwable) = new Observable[A] {
        val delegate: Observable[Unit] = Observable[Unit](Seq(()))
           override def subscribe(observer: Observer[_ >: A]): Unit =
             delegate.subscribe(
               new Observer[Unit] {
                 override def onError(throwable: Throwable): Unit =
                   observer.onError(throwable)

                override def onSubscribe(sub: Subscription): Unit =
                   observer.onSubscribe(sub)

                override def onComplete(): Unit =
                   () // not reached

                override def onNext(tResult: Unit): Unit =
                   onError(e)
               }
             )
        }

       

      It would seem resolvable by changing `onNext` in MapObservable  to

      override def onNext(tResult: T): Unit =
        try {
           observer.onNext(s(tResult))
         } catch {
           case t: Throwable => observer.onError(f(t))
         }

      and FlatMapObservable similarly.

            Assignee:
            ross@mongodb.com Ross Lawley
            Reporter:
            colin.fairless@digital.hmrc.gov.uk Colin Fairless
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: