better job handling

This commit is contained in:
Evgenii Alekseev 2019-11-18 23:22:23 +03:00
parent 9faceb4f61
commit ab790e87ff
3 changed files with 15 additions and 3 deletions

View File

@ -18,6 +18,9 @@ import spray.json._
trait HttpHandler extends StrictLogging { this: JsonSupport => trait HttpHandler extends StrictLogging { this: JsonSupport =>
implicit def exceptionHandler: ExceptionHandler = ExceptionHandler { implicit def exceptionHandler: ExceptionHandler = ExceptionHandler {
case ex: IllegalArgumentException =>
complete(StatusCodes.BadRequest, ErrorResponse(ex.getMessage))
case other: Exception => case other: Exception =>
logger.error("exception during request completion", other) logger.error("exception during request completion", other)
complete(StatusCodes.InternalServerError, ErrorResponse("unknown server error")) complete(StatusCodes.InternalServerError, ErrorResponse("unknown server error"))

View File

@ -100,5 +100,10 @@ object Job {
Seq(PLD, WAR, DRK, GNB, WHM, SCH, AST, MNK, DRG, NIN, SAM, BRD, MCH, DNC, BLM, SMN, RDM) 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) 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")
}
} }

View File

@ -12,8 +12,12 @@ class JobTest extends WordSpecLike with Matchers with BeforeAndAfterAll {
} }
} }
"return AnyJob on unknown job" in { "return AnyJob" in {
Job.withName("random string") shouldEqual Job.AnyJob 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 { "equal AnyJob to others" in {