Repository: scala-steward-org/scala-steward
Pull request #1849 (2020-12-30 15:25:25)

Use log level WARN for expected and ignored errors


Patch:
diff --git a/modules/core/src/main/scala/org/scalasteward/core/application/StewardAlg.scala b/modules/core/src/main/scala/org/scalasteward/core/application/StewardAlg.scala
index 205cecfc..85f26800 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/application/StewardAlg.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/application/StewardAlg.scala
@@ -85,7 +85,7 @@ final class StewardAlg[F[_]](config: Config)(implicit
   private def steward(repo: Repo): F[Either[Throwable, Unit]] = {
     val label = s"Steward ${repo.show}"
     logger.infoTotalTime(label) {
-      logger.attemptLogInfo(util.string.lineLeftRight(label), Some(label)) {
+      logger.attemptLogLabel(util.string.lineLeftRight(label), Some(label)) {
         F.guarantee {
           for {
             (cache, fork) <- repoCacheAlg.checkCache(repo)
diff --git a/modules/core/src/main/scala/org/scalasteward/core/edit/hooks/HookExecutor.scala b/modules/core/src/main/scala/org/scalasteward/core/edit/hooks/HookExecutor.scala
index 5038a120..d25d599f 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/edit/hooks/HookExecutor.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/edit/hooks/HookExecutor.scala
@@ -52,7 +52,7 @@ final class HookExecutor[F[_]](implicit
     for {
       _ <- logger.info(s"Executing post-update hook for ${hook.groupId}:${hook.artifactId.name}")
       repoDir <- workspaceAlg.repoDir(repo)
-      _ <- logger.attemptLogError("Post-update hook failed") {
+      _ <- logger.attemptLogWarn("Post-update hook failed") {
         processAlg.execMaybeSandboxed(hook.useSandbox)(hook.command, repoDir)
       }
       maybeCommit <- gitAlg.commitAllIfDirty(repo, hook.commitMessage(update))
diff --git a/modules/core/src/main/scala/org/scalasteward/core/util/logger.scala b/modules/core/src/main/scala/org/scalasteward/core/util/logger.scala
index 919c844f..0fe7f2b2 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/util/logger.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/util/logger.scala
@@ -24,18 +24,25 @@ import scala.concurrent.duration.FiniteDuration
 
 object logger {
   implicit final class LoggerOps[F[_]](private val logger: Logger[F]) extends AnyVal {
-    def attemptLogInfo[A](label: String, errorLabel: Option[String] = None)(fa: F[A])(implicit
+    def attemptLogLabel[A](label: String, errorLabel: Option[String] = None)(fa: F[A])(implicit
         F: MonadThrow[F]
     ): F[Either[Throwable, A]] =
       logger.info(label) >> attemptLogError(s"${errorLabel.getOrElse(label)} failed")(fa)
 
+    def attemptLogWarn[A](message: String)(fa: F[A])(implicit
+        F: MonadThrow[F]
+    ): F[Either[Throwable, A]] =
+      attemptLogImpl(fa, logger.warn(_)(message))
+
     def attemptLogError[A](message: String)(fa: F[A])(implicit
         F: MonadThrow[F]
     ): F[Either[Throwable, A]] =
-      fa.attempt.flatTap {
-        case Left(t)  => logger.error(t)(message)
-        case Right(_) => F.unit
-      }
+      attemptLogImpl(fa, logger.error(_)(message))
+
+    private def attemptLogImpl[A](fa: F[A], log: Throwable => F[Unit])(implicit
+        F: MonadThrow[F]
+    ): F[Either[Throwable, A]] =
+      fa.attempt.flatTap(_.fold(log, _ => F.unit))
 
     def infoTimed[A](msg: FiniteDuration => String)(fa: F[A])(implicit
         dateTimeAlg: DateTimeAlg[F],
diff --git a/modules/core/src/main/scala/org/scalasteward/core/vcs/VCSRepoAlg.scala b/modules/core/src/main/scala/org/scalasteward/core/vcs/VCSRepoAlg.scala
index c10536a8..11694d35 100644
--- a/modules/core/src/main/scala/org/scalasteward/core/vcs/VCSRepoAlg.scala
+++ b/modules/core/src/main/scala/org/scalasteward/core/vcs/VCSRepoAlg.scala
@@ -39,7 +39,7 @@ final class VCSRepoAlg[F[_]](config: Config)(implicit
         else logger.info(s"Clone and synchronize ${repo.show}")
       _ <- clone(repo, repoOut)
       _ <- syncFork(repo, repoOut)
-      _ <- logger.attemptLogError("Initializing and cloning submodules failed") {
+      _ <- logger.attemptLogWarn("Initializing and cloning submodules failed") {
         gitAlg.initSubmodules(repo)
       }
     } yield ()


Test patch:
diff --git a/modules/core/src/test/scala/org/scalasteward/core/util/loggerTest.scala b/modules/core/src/test/scala/org/scalasteward/core/util/loggerTest.scala
index df1cbd28..eccf1767 100644
--- a/modules/core/src/test/scala/org/scalasteward/core/util/loggerTest.scala
+++ b/modules/core/src/test/scala/org/scalasteward/core/util/loggerTest.scala
@@ -1,6 +1,6 @@
 package org.scalasteward.core.util
 
-import cats.effect.Sync
+import cats.{Applicative, ApplicativeThrow}
 import munit.FunSuite
 import org.scalasteward.core.mock.MockContext._
 import org.scalasteward.core.mock.{MockEff, MockState}
@@ -11,10 +11,9 @@ class loggerTest extends FunSuite {
     final case class Err(msg: String) extends Throwable(msg)
     val err = Err("hmm?")
     val state = mockLogger
-      .attemptLogInfo("run")(Sync[MockEff].raiseError(err))
+      .attemptLogLabel("run")(ApplicativeThrow[MockEff].raiseError(err))
       .runS(MockState.empty)
       .unsafeRunSync()
-
     assertEquals(state.logs, Vector((None, "run"), (Some(err), "run failed")))
   }
 
@@ -23,7 +22,14 @@ class loggerTest extends FunSuite {
       .infoTimed(_ => "timed")(mockLogger.info("inner"))
       .runS(MockState.empty)
       .unsafeRunSync()
-
     assertEquals(state.logs, Vector((None, "inner"), (None, "timed")))
   }
+
+  test("infoTotalTime") {
+    val state = mockLogger
+      .infoTotalTime("run")(Applicative[MockEff].unit)
+      .runS(MockState.empty)
+      .unsafeRunSync()
+    assertEquals(state.logs.size, 1)
+  }
 }