handle invalid searches without 500

pull/9444/head
Thibault Duplessis 2021-07-21 11:39:02 +02:00
parent 74d4d8689b
commit d01627edb4
2 changed files with 12 additions and 5 deletions

View File

@ -35,11 +35,13 @@ final class ESClientHttp(
def search[Q: Writes](query: Q, from: From, size: Size) =
monitor("search") {
HTTP(s"search/${index.name}/${from.value}/${size.value}", query, SearchResponse.apply)
.dmap(~_)
}
def count[Q: Writes](query: Q) =
monitor("count") {
HTTP(s"count/${index.name}", query, CountResponse.apply)
.dmap(~_)
}
def deleteById(id: lila.search.Id) =
@ -64,12 +66,13 @@ final class ESClientHttp(
def refresh =
HTTP(s"refresh/${index.name}", Json.obj())
private[search] def HTTP[D: Writes, R](url: String, data: D, read: String => R): Fu[R] =
private[search] def HTTP[D: Writes, R](url: String, data: D, read: String => R): Fu[Option[R]] =
ws.url(s"${config.endpoint}/$url").post(Json toJson data) flatMap {
case res if res.status == 200 => fuccess(read(res.body))
case res if res.status == 200 => fuccess(read(res.body).some)
case res if res.status == 400 => fuccess(none)
case res => fufail(s"$url ${res.status}")
}
private[search] def HTTP(url: String, data: JsObject): Funit = HTTP(url, data, _ => ())
private[search] def HTTP(url: String, data: JsObject): Funit = HTTP(url, data, _ => ()).void
private def monitor[A](op: String)(f: Fu[A]) =
f.monTry(res => _.search.time(op, index.name, res.isSuccess))

View File

@ -1,5 +1,7 @@
package lila.search
import ornicar.scalalib.Zero
case class Index(name: String) extends AnyVal
case class Id(value: String) extends AnyVal
@ -11,13 +13,15 @@ case class Size(value: Int) extends AnyVal
case class SearchResponse(ids: List[String]) extends AnyVal
object SearchResponse {
def apply(txt: String): SearchResponse = SearchResponse(txt.split(',').toList)
def apply(txt: String): SearchResponse = SearchResponse(txt.split(',').toList)
implicit val SearchResponseZero: Zero[SearchResponse] = Zero.instance(SearchResponse(Nil))
}
case class CountResponse(count: Int) extends AnyVal
object CountResponse {
def apply(txt: String): CountResponse = CountResponse(~txt.toIntOption)
def apply(txt: String): CountResponse = CountResponse(~txt.toIntOption)
implicit val CountResponseZero: Zero[CountResponse] = Zero.instance(CountResponse(0))
}
object Date {