Skip to content

Commit

Permalink
refactor(http/errors): reorganize package
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshinorin committed Dec 1, 2024
1 parent 3997cd2 commit 5b7b799
Show file tree
Hide file tree
Showing 14 changed files with 30 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/main/scala/net/yoshinorin/qualtet/domains/Errors.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.yoshinorin.qualtet.domains.errors

import net.yoshinorin.qualtet.http.ProblemDetailsError
import net.yoshinorin.qualtet.http.errors.ProblemDetailsError

sealed trait DomainError extends Exception

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import cats.implicits.*
import net.yoshinorin.qualtet.config.SearchConfig
import net.yoshinorin.qualtet.infrastructure.db.Executer
import net.yoshinorin.qualtet.domains.errors.InvalidSearchConditions
import net.yoshinorin.qualtet.http.ProblemDetailsError
import net.yoshinorin.qualtet.http.errors.ProblemDetailsError
import net.yoshinorin.qualtet.types.Points
import net.yoshinorin.qualtet.syntax.*

Expand Down
32 changes: 17 additions & 15 deletions src/main/scala/net/yoshinorin/qualtet/http/ResponseTranslator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import org.http4s.Challenge
import org.http4s.headers.{`Content-Type`, `WWW-Authenticate`}
import com.github.plokhotnyuk.jsoniter_scala.core.*
import net.yoshinorin.qualtet.domains.errors.DomainError
import net.yoshinorin.qualtet.http.HttpError.*
import net.yoshinorin.qualtet.http.errors.*
import net.yoshinorin.qualtet.http.errors.HttpError.*
import net.yoshinorin.qualtet.http.errors.ResponseProblemDetails
import net.yoshinorin.qualtet.syntax.*

object ResponseTranslator {
Expand All @@ -17,53 +19,53 @@ object ResponseTranslator {
private def failToResponse(f: DomainError)(using req: Request[IO]): IO[Response[IO]] = {
fromDomainError(f) match {
case e: NotFound =>
NotFound(
org.http4s.dsl.io.NotFound(
ResponseProblemDetails(
title = e.title,
status = NotFound.code,
status = org.http4s.dsl.io.NotFound.code,
detail = e.detail,
instance = req.uri.toString()
).asJson,
`Content-Type`(MediaType.application.`problem+json`)
)
case e: Unauthorized =>
Unauthorized(`WWW-Authenticate`(Challenge("Bearer", "Unauthorized")))
org.http4s.dsl.io.Unauthorized(`WWW-Authenticate`(Challenge("Bearer", "Unauthorized")))
case e: UnprocessableEntity =>
UnprocessableEntity(
org.http4s.dsl.io.UnprocessableEntity(
ResponseProblemDetails(
title = e.title,
status = UnprocessableEntity.code,
status = org.http4s.dsl.io.UnprocessableEntity.code,
detail = e.detail,
instance = req.uri.toString(),
errors = e.errors
).asJson,
`Content-Type`(MediaType.application.`problem+json`)
)
case e: BadRequest =>
BadRequest(
org.http4s.dsl.io.BadRequest(
ResponseProblemDetails(
title = e.title,
status = BadRequest.code,
status = org.http4s.dsl.io.BadRequest.code,
detail = e.detail,
instance = req.uri.toString()
).asJson,
`Content-Type`(MediaType.application.`problem+json`)
)
case e: Forbidden =>
Forbidden(
org.http4s.dsl.io.Forbidden(
ResponseProblemDetails(
title = e.title,
status = Forbidden.code,
status = org.http4s.dsl.io.Forbidden.code,
detail = e.detail,
instance = req.uri.toString()
).asJson,
`Content-Type`(MediaType.application.`problem+json`)
)
case e: InternalServerError =>
InternalServerError(
org.http4s.dsl.io.InternalServerError(
ResponseProblemDetails(
title = e.title,
status = InternalServerError.code,
status = org.http4s.dsl.io.InternalServerError.code,
detail = e.detail,
instance = req.uri.toString()
).asJson,
Expand All @@ -75,7 +77,7 @@ object ResponseTranslator {
def toResponse(e: Throwable): Request[IO] ?=> IO[Response[IO]] = {
e match {
case f: DomainError => this.failToResponse(f)
case _ => InternalServerError("Internal Server Error")
case _ => org.http4s.dsl.io.InternalServerError("Internal Server Error")
}
}

Expand All @@ -86,10 +88,10 @@ object ResponseTranslator {
case Some(x) =>
Ok(x.asJson, `Content-Type`(MediaType.application.json))
case None =>
NotFound(
org.http4s.dsl.io.NotFound(
ResponseProblemDetails(
title = "Not Found",
status = NotFound.code,
status = org.http4s.dsl.io.NotFound.code,
detail = "Not Found",
instance = req.uri.toString()
).asJson,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.yoshinorin.qualtet.http
package net.yoshinorin.qualtet.http.errors

import net.yoshinorin.qualtet.domains.errors.*

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net.yoshinorin.qualtet.http
package net.yoshinorin.qualtet.http.errors

import com.github.plokhotnyuk.jsoniter_scala.macros.*
import com.github.plokhotnyuk.jsoniter_scala.core.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import net.yoshinorin.qualtet.domains.Path
import net.yoshinorin.qualtet.domains.contents.RequestContent
import net.yoshinorin.qualtet.domains.robots.Attributes
import net.yoshinorin.qualtet.fixture.Fixture.*
import net.yoshinorin.qualtet.http.ProblemDetailsError
import net.yoshinorin.qualtet.http.errors.ProblemDetailsError
import org.scalatest.wordspec.AnyWordSpec
import org.scalatest.BeforeAndAfterAll
import net.yoshinorin.qualtet.domains.errors.InvalidSearchConditions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import net.yoshinorin.qualtet.domains.authors.AuthorName
import net.yoshinorin.qualtet.domains.Path
import net.yoshinorin.qualtet.domains.contents.RequestContent
import net.yoshinorin.qualtet.domains.robots.Attributes
import net.yoshinorin.qualtet.http.ResponseProblemDetails
import net.yoshinorin.qualtet.http.errors.ResponseProblemDetails
import net.yoshinorin.qualtet.fixture.Fixture.{author, contentService, router, unsafeDecode}
import org.scalatest.wordspec.AnyWordSpec

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.http4s.headers.`Content-Type`
import org.http4s.implicits.*
import net.yoshinorin.qualtet.domains.authors.ResponseAuthor
import net.yoshinorin.qualtet.auth.ResponseToken
import net.yoshinorin.qualtet.http.ResponseProblemDetails
import net.yoshinorin.qualtet.http.errors.ResponseProblemDetails
import net.yoshinorin.qualtet.fixture.Fixture.{author, authorService, router, unsafeDecode}
import org.scalatest.wordspec.AnyWordSpec
import cats.effect.unsafe.implicits.global
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.http4s.dsl.io.*
import org.http4s.headers.`Content-Type`
import org.http4s.implicits.*
import net.yoshinorin.qualtet.domains.authors.{AuthorName, ResponseAuthor}
import net.yoshinorin.qualtet.http.ResponseProblemDetails
import net.yoshinorin.qualtet.http.errors.ResponseProblemDetails
import net.yoshinorin.qualtet.fixture.Fixture.{author, author2, authorService, router, unsafeDecode}
import org.scalatest.wordspec.AnyWordSpec
import cats.effect.unsafe.implicits.global
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import net.yoshinorin.qualtet.domains.authors.ResponseAuthor
import net.yoshinorin.qualtet.domains.Path
import net.yoshinorin.qualtet.domains.contents.{Content, ContentId, RequestContent, ResponseContent}
import net.yoshinorin.qualtet.domains.robots.Attributes
import net.yoshinorin.qualtet.http.ResponseProblemDetails
import net.yoshinorin.qualtet.http.errors.ResponseProblemDetails
import net.yoshinorin.qualtet.fixture.Fixture.*
import net.yoshinorin.qualtet.fixture.Fixture.{router => fixtureRouter}
import org.scalatest.wordspec.AnyWordSpec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import org.http4s.dsl.io.*
import org.http4s.headers.`Content-Type`
import org.http4s.implicits.*
import net.yoshinorin.qualtet.domains.contentTypes.ContentType
import net.yoshinorin.qualtet.http.ResponseProblemDetails
import net.yoshinorin.qualtet.http.errors.ResponseProblemDetails
import net.yoshinorin.qualtet.fixture.Fixture.{router, unsafeDecode}
import org.scalatest.wordspec.AnyWordSpec

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import net.yoshinorin.qualtet.domains.Path
import net.yoshinorin.qualtet.domains.contents.RequestContent
import net.yoshinorin.qualtet.domains.robots.Attributes
import net.yoshinorin.qualtet.domains.search.ResponseSearchWithCount
import net.yoshinorin.qualtet.http.ResponseProblemDetails
import net.yoshinorin.qualtet.http.errors.ResponseProblemDetails
import net.yoshinorin.qualtet.fixture.Fixture.*
import org.scalatest.wordspec.AnyWordSpec
import org.scalatest.BeforeAndAfterAll
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import org.typelevel.ci.*
import net.yoshinorin.qualtet.auth.RequestToken
import net.yoshinorin.qualtet.domains.authors.ResponseAuthor
import net.yoshinorin.qualtet.domains.series.{RequestSeries, Series, SeriesName}
import net.yoshinorin.qualtet.http.ResponseProblemDetails
import net.yoshinorin.qualtet.http.errors.ResponseProblemDetails
import net.yoshinorin.qualtet.fixture.Fixture.*
import net.yoshinorin.qualtet.fixture.Fixture.{authProvider => fixtureAuthProvider}
import org.scalatest.wordspec.AnyWordSpec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import net.yoshinorin.qualtet.auth.RequestToken
import net.yoshinorin.qualtet.domains.articles.ResponseArticleWithCount
import net.yoshinorin.qualtet.domains.authors.ResponseAuthor
import net.yoshinorin.qualtet.domains.tags.{ResponseTag, TagId}
import net.yoshinorin.qualtet.http.ResponseProblemDetails
import net.yoshinorin.qualtet.http.errors.ResponseProblemDetails
import net.yoshinorin.qualtet.fixture.Fixture.*
import net.yoshinorin.qualtet.fixture.Fixture.{authProvider => fixtureAuthProvider}
import org.scalatest.wordspec.AnyWordSpec
Expand Down

0 comments on commit 5b7b799

Please sign in to comment.