Skip to content

Commit

Permalink
Support error handler for PlaySearchClient
Browse files Browse the repository at this point in the history
  • Loading branch information
lenguyenthanh committed Jun 2, 2024
1 parent 1546900 commit 64e5462
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
41 changes: 28 additions & 13 deletions modules/client/src/main/scala/PlaySearchClient.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import scala.concurrent.Future
import smithy4s.json.Json.given
import smithy4s.schema.Schema
import play.api.libs.ws.BodyReadable
import scala.util.control.NoStackTrace

enum SearchError extends NoStackTrace:
case BadRequest(message: String)
case InternalServerError(message: String)

class PlaySearchClient(client: StandaloneWSClient, baseUrl: String)(using ExecutionContext)
extends SearchClient:
Expand Down Expand Up @@ -52,28 +57,38 @@ class PlaySearchClient(client: StandaloneWSClient, baseUrl: String)(using Execut
request(s"$baseUrl/search/$from/$size", SearchInput(query))

private def request[D: Schema, R: Schema](url: String, data: D): Future[R] =
client
.url(url)
.post(data)
.flatMap:
case res if res.status == 200 => Future(res.body[R])
case res => Future.failed(Exception(s"$url ${res.status} ${res.body}"))
try
client
.url(url)
.post(data)
.flatMap:
case res if res.status == 200 => Future(res.body[R])
case res if res.status == 400 => Future.failed(SearchError.BadRequest(s"$url ${res.status} ${res.body}"))
case res => Future.failed(SearchError.InternalServerError(s"$url ${res.status} ${res.body}"))
catch
case e: JsonWriterException => Future.failed(SearchError.BadRequest(e.getMessage))

private def request_[D: Schema](url: String, data: D): Future[Unit] =
client
.url(url)
.post(data)
.flatMap:
case res if res.status == 200 => Future(())
case res => Future.failed(Exception(s"$url ${res.status} ${res.body}"))
try
client
.url(url)
.post(data)
.flatMap:
case res if res.status == 200 => Future(())
case res if res.status == 400 => Future.failed(SearchError.BadRequest(s"$url ${res.status} ${res.body}"))
case res => Future.failed(SearchError.InternalServerError(s"$url ${res.status} ${res.body}"))
catch
case e: JsonWriterException =>
Future.failed(SearchError.BadRequest(e.getMessage))

private def request_(url: String): Future[Unit] =
client
.url(url)
.execute("POST")
.flatMap:
case res if res.status == 200 => Future(())
case res => Future.failed(Exception(s"$url ${res.status} ${res.body}"))
case res if res.status == 400 => Future.failed(SearchError.BadRequest(s"$url ${res.status} ${res.body}"))
case res => Future.failed(SearchError.InternalServerError(s"$url ${res.status} ${res.body}"))

final private case class SearchInput(query: Query)
final private case class SourceInput(source: Source)
Expand Down
14 changes: 14 additions & 0 deletions modules/e2e/src/test/scala/CompatSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import play.api.libs.ws.*
import play.api.libs.ws.ahc.*
import scala.concurrent.ExecutionContext.Implicits.*
import java.time.Instant
import lila.search.client.SearchError

object CompatSuite extends weaver.IOSuite:

Expand All @@ -37,6 +38,19 @@ object CompatSuite extends weaver.IOSuite:
val query = Query.Forum("foo")
IO.fromFuture(IO(client.search(query, 0, 10))).map(expect.same(_, lila.search.spec.SearchOutput(Nil)))

test("search study endpoint"): client =>
val query = Query.Study(
text =
"哈尔滨双城区《哪个酒店有小姐服务汽车站》【威信:█184-0823-1261█ 提供上门服务】面到付款 有工作室,精挑细选,各种类型,应有尽有,诚信经营,坚决不做一次性买卖!国内一二线城市均可安排💯6sFW"
.take(100),
userId = Some(value = "bla")
)
IO.fromFuture(IO(client.search(query, 0, 10)))
.handleErrorWith:
case e: SearchError.BadRequest =>
IO.pure(lila.search.spec.SearchOutput(Nil))
.map(expect.same(_, lila.search.spec.SearchOutput(Nil)))

test("count endpoint"): client =>
val query = Query.Team("foo")
IO.fromFuture(IO(client.count(query))).map(expect.same(_, lila.search.spec.CountOutput(0)))
Expand Down

0 comments on commit 64e5462

Please sign in to comment.