Skip to content

Commit

Permalink
Add getOrRethrow into feither
Browse files Browse the repository at this point in the history
  • Loading branch information
geirolz committed Oct 9, 2023
1 parent 4d464ff commit 9de5766
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 4 additions & 1 deletion shared/src/main/scala/mouse/feither.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ final class FEitherOps[F[_], L, R](private val felr: F[Either[L, R]]) extends An
def getOrElseIn[A >: R](right: => A)(implicit F: Functor[F]): F[A] =
F.map(felr)(_.fold(_ => right, identity))

def getOrRaise[E](e: => E)(implicit F: MonadError[F, _ >: E]): F[R] =
def getOrRethrow(implicit F: MonadThrow[F], env: L <:< Throwable): F[R] =
foldF(e => F.raiseError[R](e))(F.pure)

def getOrRaise[E](e: => E)(implicit F: MonadError[F, ? >: E]): F[R] =
getOrElseF(F.raiseError(e))

def getOrRaiseMsg(msg: => String)(implicit F: MonadThrow[F]): F[R] =
Expand Down
7 changes: 7 additions & 0 deletions shared/src/test/scala/mouse/FEitherSyntaxTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ class FEitherSyntaxTest extends MouseSuite {
assertEquals(leftValue.getOrElseIn(0), List(0))
}

test("FEitherSyntax.getOrRethrow"){
val ex1 = new RuntimeException("BOOM 1!")
assertEquals(Try(1.asRight).getOrRethrow, Success(1))
assertEquals(Try(ex1.asLeft).getOrRethrow, Failure(ex1))
assertEquals(Try[Either[Throwable, Int]](throw ex1).getOrRethrow, Failure(ex1))
}

test("FEitherSyntax.getOrRaise") {
val ex1 = new RuntimeException("BOOM 1!")
val ex2 = new RuntimeException("BOOM 2!")
Expand Down

0 comments on commit 9de5766

Please sign in to comment.