Try tapFailure

(, en)

Add tapFailure to make logging easier:

// src/main/scala/ErrorUtil.scala
import scala.util.{Failure, Try}

object ErrorUtil {
  implicit class tryTapFailure[A](t: Try[A]) {
    def tapFailure(f: Throwable => Unit): Try[A] = t match {
      case Failure(ex) => f(ex); t
      case _ => t
    }
  }
}
// src/test/scala/TryTapFailureSpec.scala
def somethingStupid = throw new RuntimeException("ABC")

val result = Try(somethingStupid)
  .tapFailure(ex => logger.error(ex)("I did something stupid"))
  .getOrElse("default")

logger.info(result)

Or use this when you don’t want the implicit construction:

// src/test/scala/TryTapFailureSpec.scala
def somethingStupid = throw new RuntimeException("ABC")

val result = Try(somethingStupid)
  .tap {
    case Failure(ex) => logger.error(ex)("I did something stupid")
    case _           =>
  }
  .getOrElse("default")

logger.info(result)