-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use tapir to describe and document routes
- Loading branch information
Showing
26 changed files
with
385 additions
and
338 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ SortImports.blocks = [ | |
"java." | ||
"scala." | ||
"org.http4s." | ||
"sttp." | ||
"cats." | ||
"io.circe." | ||
"doobie." | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,34 @@ | ||
package car.advert.http | ||
|
||
import org.http4s.Response | ||
import org.http4s.circe.CirceEntityDecoder | ||
import org.http4s.circe.CirceEntityEncoder | ||
import org.http4s.dsl.Http4sDsl | ||
|
||
import cats.effect.Async | ||
import cats.implicits._ | ||
|
||
import io.circe.Encoder | ||
import io.circe.syntax._ | ||
import com.colisweb.tracing.core.TracingContext | ||
|
||
import car.advert.model.error.AppError | ||
import car.advert.model.error.AppError.InternalServerError | ||
import car.advert.model.error.Error_OUT | ||
import io.chrisdavenport.log4cats.Logger | ||
|
||
abstract class BaseHttp4sRoute[F[_]: Async: Logger] extends Http4sDsl[F] with CirceEntityEncoder with CirceEntityDecoder { | ||
|
||
implicit class FToResponse[A](result: F[A]) { | ||
def toResponse(implicit encoder: Encoder[A]): F[Response[F]] = | ||
result | ||
.flatMap { | ||
case _: Unit => Ok() | ||
case model => Ok(model.asJson) | ||
} | ||
.recoverWith { | ||
case appError: AppError => appError.toHttpResponse() | ||
case t: Exception => | ||
println(t) | ||
Logger[F].error(t)(message = "Unexpected exception") *> InternalServerError("Internal Server Error") | ||
} | ||
private def exceptionToErrorOut(t: Throwable)(implicit tracingContext: TracingContext[F]): F[Error_OUT] = | ||
t match { | ||
case appError: AppError => appError.toErrorOut.pure[F] | ||
case _ => | ||
Logger[F].error(t)(message = "Unexpected exception") *> | ||
new InternalServerError("Unexpected exception").toErrorOut.pure[F] | ||
} | ||
|
||
def toResponse(implicit tracingContext: TracingContext[F]): F[Either[Error_OUT, A]] = | ||
result.map(t => t.asRight[Error_OUT]).recoverWith { | ||
case t: Throwable => exceptionToErrorOut(t).map(_.asLeft[A]) | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.