Why is Akka Streams swallowing my exceptions?

前端 未结 3 1013
悲&欢浪女
悲&欢浪女 2021-02-04 03:38

Why is the exception in

import akka.actor.ActorSystem
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.Source

object TestExceptionHandling {
  d         


        
3条回答
  •  深忆病人
    2021-02-04 03:51

    I had a different issue with Akka Streams swallowing my exceptions. I'll post it here since this is the top Google result.

    In a case like this, where source is a Source[ByteString, Any]:

    source.runWith(StreamConverters.fromOutputStream(() => outputStream))
    

    This returns a Future[IOResult]. If the write to the output stream fails (for example, the source fails), then the Future will still return a Success. In this case, you actually have to check the IOResult for the error:

    source.runWith(StreamConverters.fromOutputStream(() => output)).
          map(ior => {
            if (!ior.wasSuccessful) 
              throw new RuntimeException(ior.getError)
          })
    

    The result of this will be a failed Future with the correct exception.

提交回复
热议问题