From ab790e87ff06c1ab3897a7aa0281a41e18b11a9d Mon Sep 17 00:00:00 2001 From: Evgeniy Alekseev Date: Mon, 18 Nov 2019 23:22:23 +0300 Subject: [PATCH] better job handling --- .../me/arcanis/ffxivbis/http/api/v1/HttpHandler.scala | 3 +++ src/main/scala/me/arcanis/ffxivbis/models/Job.scala | 7 ++++++- src/test/scala/me/arcanis/ffxivbis/models/JobTest.scala | 8 ++++++-- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main/scala/me/arcanis/ffxivbis/http/api/v1/HttpHandler.scala b/src/main/scala/me/arcanis/ffxivbis/http/api/v1/HttpHandler.scala index ff1758b..a34140a 100644 --- a/src/main/scala/me/arcanis/ffxivbis/http/api/v1/HttpHandler.scala +++ b/src/main/scala/me/arcanis/ffxivbis/http/api/v1/HttpHandler.scala @@ -18,6 +18,9 @@ import spray.json._ trait HttpHandler extends StrictLogging { this: JsonSupport => implicit def exceptionHandler: ExceptionHandler = ExceptionHandler { + case ex: IllegalArgumentException => + complete(StatusCodes.BadRequest, ErrorResponse(ex.getMessage)) + case other: Exception => logger.error("exception during request completion", other) complete(StatusCodes.InternalServerError, ErrorResponse("unknown server error")) diff --git a/src/main/scala/me/arcanis/ffxivbis/models/Job.scala b/src/main/scala/me/arcanis/ffxivbis/models/Job.scala index 716084d..3085d78 100644 --- a/src/main/scala/me/arcanis/ffxivbis/models/Job.scala +++ b/src/main/scala/me/arcanis/ffxivbis/models/Job.scala @@ -100,5 +100,10 @@ object Job { Seq(PLD, WAR, DRK, GNB, WHM, SCH, AST, MNK, DRG, NIN, SAM, BRD, MCH, DNC, BLM, SMN, RDM) lazy val availableWithAnyJob: Seq[Job] = available.prepended(AnyJob) - def withName(job: String): Job.Job = available.find(_.toString == job.toUpperCase).getOrElse(AnyJob) + def withName(job: String): Job.Job = + availableWithAnyJob.find(_.toString.equalsIgnoreCase(job.toUpperCase)) match { + case Some(value) => value + case None if job.isEmpty => AnyJob + case _ => throw new IllegalArgumentException("Invalid or unknown job") + } } diff --git a/src/test/scala/me/arcanis/ffxivbis/models/JobTest.scala b/src/test/scala/me/arcanis/ffxivbis/models/JobTest.scala index 874c070..da5c2ef 100644 --- a/src/test/scala/me/arcanis/ffxivbis/models/JobTest.scala +++ b/src/test/scala/me/arcanis/ffxivbis/models/JobTest.scala @@ -12,8 +12,12 @@ class JobTest extends WordSpecLike with Matchers with BeforeAndAfterAll { } } - "return AnyJob on unknown job" in { - Job.withName("random string") shouldEqual Job.AnyJob + "return AnyJob" in { + Job.withName("anyjob") shouldEqual Job.AnyJob + } + + "fail on unknown job" in { + an [IllegalArgumentException] should be thrownBy Job.withName("random string") } "equal AnyJob to others" in {