2018-10-29 17:19:37 +08:00
|
|
|
/*
|
2019-01-02 18:55:26 +08:00
|
|
|
* Copyright (C) 2016-2019 Lightbend Inc. <https://www.lightbend.com>
|
2016-01-21 18:06:42 +02:00
|
|
|
*/
|
2018-03-13 23:45:55 +09:00
|
|
|
|
2016-02-16 16:56:06 +01:00
|
|
|
package akka.stream
|
2016-01-21 18:06:42 +02:00
|
|
|
|
|
|
|
|
import akka.Done
|
2018-02-20 13:49:31 +02:00
|
|
|
|
|
|
|
|
import scala.util.control.NoStackTrace
|
2016-01-21 18:06:42 +02:00
|
|
|
import scala.util.{ Failure, Success, Try }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Holds a result of an IO operation.
|
|
|
|
|
*
|
|
|
|
|
* @param count Numeric value depending on context, for example IO operations performed or bytes processed.
|
|
|
|
|
* @param status Status of the result. Can be either [[akka.Done]] or an exception.
|
|
|
|
|
*/
|
2016-10-24 17:00:51 +02:00
|
|
|
final case class IOResult(count: Long, status: Try[Done]) {
|
|
|
|
|
|
|
|
|
|
def withCount(value: Long): IOResult = copy(count = value)
|
|
|
|
|
def withStatus(value: Try[Done]): IOResult = copy(status = value)
|
2016-01-21 18:06:42 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Java API: Numeric value depending on context, for example IO operations performed or bytes processed.
|
|
|
|
|
*/
|
|
|
|
|
def getCount: Long = count
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Java API: Indicates whether IO operation completed successfully or not.
|
|
|
|
|
*/
|
|
|
|
|
def wasSuccessful: Boolean = status.isSuccess
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Java API: If the IO operation resulted in an error, returns the corresponding [[Throwable]]
|
|
|
|
|
* or throws [[UnsupportedOperationException]] otherwise.
|
|
|
|
|
*/
|
|
|
|
|
def getError: Throwable = status match {
|
2019-02-09 15:25:39 +01:00
|
|
|
case Failure(t) => t
|
|
|
|
|
case Success(_) => throw new UnsupportedOperationException("IO operation was successful.")
|
2016-01-21 18:06:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2016-10-24 17:00:51 +02:00
|
|
|
|
|
|
|
|
object IOResult {
|
|
|
|
|
|
|
|
|
|
/** JAVA API: Creates successful IOResult */
|
|
|
|
|
def createSuccessful(count: Long): IOResult =
|
|
|
|
|
new IOResult(count, Success(Done))
|
|
|
|
|
|
|
|
|
|
/** JAVA API: Creates failed IOResult, `count` should be the number of bytes (or other unit, please document in your APIs) processed before failing */
|
|
|
|
|
def createFailed(count: Long, ex: Throwable): IOResult =
|
|
|
|
|
new IOResult(count, Failure(ex))
|
|
|
|
|
}
|
2018-02-20 13:49:31 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This exception signals that a stream has been completed by an onError signal
|
|
|
|
|
* while there was still IO operations in progress.
|
|
|
|
|
*/
|
|
|
|
|
final case class AbruptIOTerminationException(ioResult: IOResult, cause: Throwable)
|
|
|
|
|
extends RuntimeException("Stream terminated without completing IO operation.", cause) with NoStackTrace
|