Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SiriusResult should wrap Throwable (issue #8) #25

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions src/main/scala/com/comcast/xfinity/sirius/api/SiriusResult.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

/**
Expand All @@ -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?
Expand All @@ -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")
}
Expand All @@ -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")
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand All @@ -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
}
}
}
}
}