score:24

Accepted answer

From the comment under the question, the real problem emerges:

 val questionOption = Question.getQuestionForQuestionId(userExam.get.examId, currQuesId + 1) 
 if(questionOption.isEmpty) { 
   Left(Failure(FailureCode.NO_DATA_FOUND, "Cannot get next exam question you tampered your cookie or cookie is lost.... >> TODO... modify the exception message")) 
 }

By itself, if returns type Unit so that your statement is returning nothing useful. If you want to return something you need to add in either an else which then returns the least upper bound of the result types. Hence

 >>> val yo = if(1 != 0) 4
 yo: Unit

 >>> val ya = if(1 != 0) Left(1) else Right("got it")
 ya: Either[Int, String]

score:14

You could just do a boolean check to see of the value is None and throw the error to the caller if it is, otherwise continue processing:

scala> val o: Option[Any] = None
o: Option[Any] = None

scala> println(o == None)
true

scala> println(o != None)
false

But maybe a better way to accomplish what you're trying to do, alert the caller of the error or continue processing, would be to use Scala's Try idiom to handle errors.


Related Query

More Query from same tag