mirror of
https://github.com/dyrkin/luxmed-bot.git
synced 2025-12-26 23:21:40 +01:00
properly process api errors
This commit is contained in:
@@ -3,7 +3,7 @@ package com.lbs.api
|
||||
|
||||
import com.lbs.api.exception.{ApiException, GenericException, InvalidLoginOrPasswordException, ServiceIsAlreadyBookedException}
|
||||
import com.lbs.api.json.JsonSerializer.extensions._
|
||||
import com.lbs.api.json.model.{LuxmedBaseError, LuxmedErrors, LuxmedError, SerializableJsonObject}
|
||||
import com.lbs.api.json.model._
|
||||
import com.lbs.common.Logger
|
||||
import scalaj.http.{HttpRequest, HttpResponse}
|
||||
|
||||
@@ -56,24 +56,23 @@ package object http extends Logger {
|
||||
}
|
||||
|
||||
private def luxmedErrorToApiException[T <: LuxmedBaseError](ler: HttpResponse[T]): ApiException = {
|
||||
val genericException = ler.body match {
|
||||
case e: LuxmedErrors =>
|
||||
new GenericException(ler.code, ler.statusLine, e.errors.values.mkString("; "))
|
||||
case e: LuxmedError =>
|
||||
new GenericException(ler.code, ler.statusLine, e.message)
|
||||
}
|
||||
|
||||
val errorMessage = genericException.message.toLowerCase
|
||||
val message = ler.body.message
|
||||
val errorMessage = message.toLowerCase
|
||||
if (errorMessage.contains("invalid login or password"))
|
||||
new InvalidLoginOrPasswordException
|
||||
else if (errorMessage.contains("already booked this service"))
|
||||
new ServiceIsAlreadyBookedException
|
||||
else genericException
|
||||
else
|
||||
new GenericException(ler.code, ler.statusLine, message)
|
||||
}
|
||||
|
||||
private def extractLuxmedError(httpResponse: Try[HttpResponse[String]]) = {
|
||||
httpResponse.flatMap(response => Try(response.asEntity[LuxmedErrors]).map(luxmedErrorToApiException).
|
||||
orElse(Try(response.asEntity[LuxmedError]).map(luxmedErrorToApiException))).toOption
|
||||
httpResponse.flatMap { response =>
|
||||
Try(response.asEntity[LuxmedErrorsMap])
|
||||
.orElse(Try(response.asEntity[LuxmedErrorsList]))
|
||||
.orElse(Try(response.asEntity[LuxmedError]))
|
||||
.map(e => luxmedErrorToApiException(e.asInstanceOf[HttpResponse[LuxmedBaseError]]))
|
||||
}.toOption
|
||||
}
|
||||
|
||||
private def hidePasswords(httpRequest: HttpRequest) = {
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
|
||||
package com.lbs.api.json.model
|
||||
|
||||
trait LuxmedBaseError
|
||||
trait LuxmedBaseError {
|
||||
def message: String
|
||||
}
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
|
||||
package com.lbs.api.json.model
|
||||
|
||||
case class LuxmedErrors(errors: Map[String, List[String]]) extends SerializableJsonObject with LuxmedBaseError
|
||||
@@ -0,0 +1,8 @@
|
||||
|
||||
package com.lbs.api.json.model
|
||||
|
||||
case class LuxmedErrorsList(errors: List[LuxmedErrorsListElement]) extends SerializableJsonObject with LuxmedBaseError {
|
||||
override def message: String = errors.map(_.message).mkString("; ")
|
||||
}
|
||||
|
||||
case class LuxmedErrorsListElement(errorCode: Int, message: String, additionalData: Map[String, String]) extends SerializableJsonObject
|
||||
@@ -0,0 +1,6 @@
|
||||
|
||||
package com.lbs.api.json.model
|
||||
|
||||
case class LuxmedErrorsMap(errors: Map[String, List[String]]) extends SerializableJsonObject with LuxmedBaseError {
|
||||
override def message: String = errors.values.mkString("; ")
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.lbs.api.json.model
|
||||
|
||||
import com.lbs.api.json.JsonSerializer.extensions._
|
||||
import org.scalatest.{FunSuiteLike, Matchers}
|
||||
|
||||
class LuxmedErrorsListSpec extends FunSuiteLike with Matchers with CommonSpec {
|
||||
|
||||
test("deserialization") {
|
||||
val json =
|
||||
"""
|
||||
|{"Errors":[{"ErrorCode":16000006,"Message":"You have already booked this service","AdditionalData":{}}]}
|
||||
""".stripMargin
|
||||
|
||||
val response = json.as[LuxmedErrorsList]
|
||||
|
||||
response should be(LuxmedErrorsList(List(LuxmedErrorsListElement(16000006, "You have already booked this service", Map.empty))))
|
||||
}
|
||||
}
|
||||
@@ -1,17 +1,17 @@
|
||||
package com.lbs.api.json.model
|
||||
|
||||
import org.scalatest.{FunSuiteLike, Matchers}
|
||||
import com.lbs.api.json.JsonSerializer.extensions._
|
||||
import org.scalatest.{FunSuiteLike, Matchers}
|
||||
|
||||
class LuxmedErrorsSpec extends FunSuiteLike with Matchers with CommonSpec {
|
||||
class LuxmedErrorsMapSpec extends FunSuiteLike with Matchers with CommonSpec {
|
||||
test("deserialization") {
|
||||
val json =
|
||||
"""
|
||||
|{"Errors":{"ToDate.Date":["'To Date. Date' must be greater than or equal to '06/04/2018 00:00:00'."]}}
|
||||
""".stripMargin
|
||||
|
||||
val response = json.as[LuxmedErrors]
|
||||
val response = json.as[LuxmedErrorsMap]
|
||||
|
||||
response should be(LuxmedErrors(Map("toDate.Date" -> List("'To Date. Date' must be greater than or equal to '06/04/2018 00:00:00'."))))
|
||||
response should be(LuxmedErrorsMap(Map("toDate.Date" -> List("'To Date. Date' must be greater than or equal to '06/04/2018 00:00:00'."))))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user