only expose client errors in the bot/board API

pull/6154/head
Thibault Duplessis 2020-03-11 15:18:23 -06:00
parent 11b1dab2da
commit 0a930f1da0
3 changed files with 14 additions and 6 deletions

View File

@ -95,13 +95,13 @@ final class PlayApi(
case Array("game", id, "abort") =>
as(id, me) { pov =>
env.bot.player.abort(pov) inject jsonOkResult recover {
case e: lila.base.LilaException => BadRequest(e.getMessage)
case e: lila.base.ClientError => BadRequest(e.getMessage)
}
}
case Array("game", id, "resign") =>
as(id, me) { pov =>
env.bot.player.resign(pov) inject jsonOkResult recover {
case e: lila.base.LilaException => BadRequest(e.getMessage)
case e: lila.base.ClientError => BadRequest(e.getMessage)
}
}
case _ => notFoundJson("No such command")

View File

@ -18,11 +18,13 @@ final class BotPlayer(
isOfferingRematch: lila.round.IsOfferingRematch
)(implicit ec: scala.concurrent.ExecutionContext, system: akka.actor.ActorSystem) {
private def clientError[A](msg: String): Fu[A] = fufail(lila.common.base.LilaException.client(msg))
def apply(pov: Pov, me: User, uciStr: String, offeringDraw: Option[Boolean]): Funit =
lila.common.Future.delay((pov.game.hasAi ?? 500) millis) {
Uci(uciStr).fold(fufail[Unit](s"Invalid UCI: $uciStr")) { uci =>
Uci(uciStr).fold(clientError[Unit](s"Invalid UCI: $uciStr")) { uci =>
lila.mon.bot.moves(me.username).increment()
if (!pov.isMyTurn) fufail("Not your turn, or game already over")
if (!pov.isMyTurn) clientError("Not your turn, or game already over")
else {
val promise = Promise[Unit]
if (pov.player.isOfferingDraw && (offeringDraw contains false)) declineDraw(pov)
@ -68,7 +70,7 @@ final class BotPlayer(
}
def abort(pov: Pov): Funit =
if (!pov.game.abortable) fufail("This game can no longer be aborted")
if (!pov.game.abortable) clientError("This game can no longer be aborted")
else
fuccess {
Bus.publish(
@ -84,7 +86,7 @@ final class BotPlayer(
Tell(pov.gameId, Resign(pov.playerId)),
"roundMapTell"
)
} else fufail("This game cannot be resigned")
} else clientError("This game cannot be resigned")
def declineDraw(pov: Pov): Unit =
if (pov.game.drawable && pov.opponent.isOfferingDraw)

View File

@ -9,6 +9,8 @@ trait LilaException extends Exception {
override def toString = message
}
trait ClientError extends LilaException
object LilaException extends scalaz.syntax.ToShowOps {
def apply(msg: String) = new LilaException {
@ -16,4 +18,8 @@ object LilaException extends scalaz.syntax.ToShowOps {
}
def apply(msg: Failures): LilaException = apply(msg.shows)
def client(msg: String) = new ClientError {
val message = msg
}
}