diff --git a/src/main/scala/com/comcast/xfinity/sirius/api/SiriusResult.scala b/src/main/scala/com/comcast/xfinity/sirius/api/SiriusResult.scala index e94e3bad..5a975687 100644 --- a/src/main/scala/com/comcast/xfinity/sirius/api/SiriusResult.scala +++ b/src/main/scala/com/comcast/xfinity/sirius/api/SiriusResult.scala @@ -41,8 +41,16 @@ object SiriusResult { * * @return SiriusResult */ - def error(rte: RuntimeException): SiriusResult = SiriusResult(Left(rte)) - + def error(rte: RuntimeException): SiriusResult = exception(rte) + + /** + * Factory method for creating a SiriusResult with an exception. + * + * @param t the Throwable to wrap + * + * @return SiriusResult + */ + def exception(t: Throwable): SiriusResult = SiriusResult(Left(t)) } /** @@ -53,7 +61,7 @@ object SiriusResult { * methods {@link SiriusResult#some()} and {@link SiriusResult#none()} */ // TODO: hide this within the scope of the companion object? -case class SiriusResult(private val value: Either[RuntimeException, Option[Object]]) { +case class SiriusResult(private val value: Either[Throwable, Option[Object]]) { /** * Does this result contain a value? @@ -73,7 +81,7 @@ case class SiriusResult(private val value: Either[RuntimeException, Option[Objec * @throws IllegalStateException if no such value exists */ def getValue: Object = value match { - case Left(rte) => throw rte + case Left(t) => throw t case Right(Some(v)) => v case Right(None) => throw new IllegalStateException("Result has no value") } @@ -82,4 +90,17 @@ case class SiriusResult(private val value: Either[RuntimeException, Option[Objec * @return true if this instance wraps an exception */ def isError: Boolean = value.isLeft + + /** + * Retrieves the exception associated with this result. If an exception + * has not been set an IllegalStateException is thrown. + * + * @return the Throwable wrapped by this instance if it exists + * @throws IllegalStateException if no such Throwable exists + */ + def getException: Throwable = value match { + case Left(t) => t + case _ => throw new IllegalStateException("Result has no exception") + } + } diff --git a/src/test/scala/com/comcast/xfinity/sirius/api/SiriusResultTest.scala b/src/test/scala/com/comcast/xfinity/sirius/api/SiriusResultTest.scala index f2af65c2..37c37c12 100644 --- a/src/test/scala/com/comcast/xfinity/sirius/api/SiriusResultTest.scala +++ b/src/test/scala/com/comcast/xfinity/sirius/api/SiriusResultTest.scala @@ -59,12 +59,12 @@ class SiriusResultTest extends NiceTest { } it("should rethrow the exception when it has an error") { - val theException = new RuntimeException() + val theThrowable = new Throwable() try { - SiriusResult.error(theException).getValue + SiriusResult.exception(theThrowable).getValue assert(false, "Exception should have been thrown") } catch { - case rte: RuntimeException => assert(theException === rte) + case t: Throwable => assert(theThrowable === t) } } } @@ -87,6 +87,19 @@ class SiriusResultTest extends NiceTest { SiriusResult.error(new RuntimeException()).isError } } + + it("should throw an IllegalStateException when it has no exception") { + intercept[IllegalStateException] { + SiriusResult.none().getException + } + } + + it("should return the exception when it has been set") { + assertResult(true) { + val t = new Throwable() + SiriusResult.exception(t).getException == t + } + } } } }