Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify ApiErrorLogger.instance #266

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions app/src/main/scala/http/ApiLogger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,18 @@ import cats.data.Kleisli
import cats.syntax.all.*
import cats.effect.IO
import org.http4s.internal.Logger as Http4sLogger
import org.http4s.{ HttpApp, Response }
import org.http4s.{ HttpApp, Request, Response }
import org.typelevel.log4cats.Logger

object ApiErrorLogger:

val logOnError: Response[IO] => Boolean = res => !res.status.isSuccess && res.status.code != 404
val isResponseError: Response[IO] => Boolean = res => !res.status.isSuccess && res.status.code != 404

private def logError(req: Request[IO], res: Response[IO])(using Logger[IO]): IO[Unit] =
Http4sLogger.logMessage(req)(true, true)(Logger[IO].warn) >>
Http4sLogger.logMessage(res)(true, true)(Logger[IO].warn)

def instance(using Logger[IO]): HttpApp[IO] => HttpApp[IO] = http =>
Kleisli: req =>
http(req).flatTap: res =>
logOnError(res)
.pure[IO]
.ifM(
Http4sLogger.logMessage(req)(true, true)(Logger[IO].warn) >>
Http4sLogger.logMessage(res)(true, true)(Logger[IO].warn),
IO.unit
)
logError(req, res).whenA(isResponseError(res))