Skip to content

Commit

Permalink
fix: Validatedを使ってエラーログをすべて出すように
Browse files Browse the repository at this point in the history
  • Loading branch information
rito528 committed May 7, 2023
1 parent 944df64 commit 9debe67
Showing 1 changed file with 13 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.github.unchama.bungeesemaphoreresponder.bukkit.listeners

import cats.{Applicative, ApplicativeError}
import cats.effect.{Async, ConcurrentEffect, Timer}
import cats.ApplicativeError
import cats.data.Validated
import cats.effect.{Async, ConcurrentEffect, Sync, Timer}
import com.github.unchama.bungeesemaphoreresponder.Configuration
import com.github.unchama.bungeesemaphoreresponder.domain.actions.BungeeSemaphoreSynchronization
import com.github.unchama.bungeesemaphoreresponder.domain.{PlayerDataFinalizer, PlayerName}
Expand All @@ -22,8 +23,6 @@ class BungeeSemaphoreCooperator[F[_]: ConcurrentEffect: Timer](
effectEnvironment: EffectEnvironment
) extends Listener {

import cats.implicits._

@EventHandler(priority = EventPriority.LOWEST)
def onQuit(event: PlayerQuitEvent): Unit = {
val player = event.getPlayer
Expand All @@ -40,23 +39,24 @@ class BungeeSemaphoreCooperator[F[_]: ConcurrentEffect: Timer](
finalizers.map(finalizer => retryUntilSucceeds(finalizer.onQuitOf(player))(10))
)

import cats.implicits._

val program = for {
raceResult <- ConcurrentEffect[F].race(timeout, quitProcess)
_ <- raceResult match {
case Left(_) =>
synchronization.notifySaveFailureOf(name) >> ApplicativeError[F, Throwable]
.raiseError[Unit](TimeoutReached)
case Right(results) =>
results.partition(_.isRight) match {
case (_, Nil) =>
results.traverse(e => Validated.fromEither(e).leftMap(List.apply(_))) match {
case Validated.Valid(_) =>
synchronization.confirmSaveCompletionOf(name)
case (_, lefts) =>
lefts.traverse {
case Left(throwable) =>
synchronization.notifySaveFailureOf(name) >> ApplicativeError[F, Throwable]
.raiseError[Unit](throwable)
case Right(_) => Applicative[F].unit
}
case Validated.Invalid(errors) =>
synchronization.notifySaveFailureOf(name) >> errors.traverse(error =>
Sync[F].delay {
error.printStackTrace()
}
)
}
}
} yield ()
Expand Down

0 comments on commit 9debe67

Please sign in to comment.