diff options
author | Kseniya Tomskikh <ktomskih@datamonsters.co> | 2017-10-30 16:29:46 +0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-30 16:29:46 +0700 |
commit | 604fbf0a7a082bc440c0778abd6f90005b210c16 (patch) | |
tree | fd846d42448b2d3f432056898f0c3433475ea0da /src/main/scala/xyz/driver/pdsuicommon/error | |
parent | 8811d60442d060097027b33784ece9a704458e1d (diff) | |
parent | d92a2795a02b9711aa9aed92a770f85377b31460 (diff) | |
download | rest-query-604fbf0a7a082bc440c0778abd6f90005b210c16.tar.gz rest-query-604fbf0a7a082bc440c0778abd6f90005b210c16.tar.bz2 rest-query-604fbf0a7a082bc440c0778abd6f90005b210c16.zip |
Merge pull request #54 from drivergroup/PDSUI-2336v0.11.0
PDSUI-2336 Cleanup
Diffstat (limited to 'src/main/scala/xyz/driver/pdsuicommon/error')
-rw-r--r-- | src/main/scala/xyz/driver/pdsuicommon/error/ErrorCode.scala | 15 | ||||
-rw-r--r-- | src/main/scala/xyz/driver/pdsuicommon/error/ErrorsResponse.scala | 78 |
2 files changed, 33 insertions, 60 deletions
diff --git a/src/main/scala/xyz/driver/pdsuicommon/error/ErrorCode.scala b/src/main/scala/xyz/driver/pdsuicommon/error/ErrorCode.scala index 5574c01..748e76a 100644 --- a/src/main/scala/xyz/driver/pdsuicommon/error/ErrorCode.scala +++ b/src/main/scala/xyz/driver/pdsuicommon/error/ErrorCode.scala @@ -1,7 +1,6 @@ package xyz.driver.pdsuicommon.error -import play.api.libs.functional.syntax._ -import play.api.libs.json.{Format, Reads, Writes} +import xyz.driver.core.json.EnumJsonFormat @SuppressWarnings(Array("org.wartremover.warts.Enumeration")) object ErrorCode extends Enumeration { @@ -9,9 +8,13 @@ object ErrorCode extends Enumeration { type ErrorCode = Value val Unspecified = Value(1) - private val fromJsonReads: Reads[ErrorCode] = Reads.of[Int].map(ErrorCode.apply) - private val toJsonWrites: Writes[ErrorCode] = Writes.of[Int].contramap(_.id) - - implicit val jsonFormat: Format[ErrorCode] = Format(fromJsonReads, toJsonWrites) + implicit val jsonFormat = new EnumJsonFormat[ErrorCode]( + "200" -> ErrorCode.Value(200), + "400" -> ErrorCode.Value(400), + "401" -> ErrorCode.Value(401), + "403" -> ErrorCode.Value(403), + "404" -> ErrorCode.Value(404), + "500" -> ErrorCode.Value(500) + ) } diff --git a/src/main/scala/xyz/driver/pdsuicommon/error/ErrorsResponse.scala b/src/main/scala/xyz/driver/pdsuicommon/error/ErrorsResponse.scala index 3761cc5..ccb84c2 100644 --- a/src/main/scala/xyz/driver/pdsuicommon/error/ErrorsResponse.scala +++ b/src/main/scala/xyz/driver/pdsuicommon/error/ErrorsResponse.scala @@ -1,18 +1,13 @@ package xyz.driver.pdsuicommon.error -import xyz.driver.pdsuicommon.json.Serialization.seqJsonFormat -import ErrorCode.{ErrorCode, Unspecified} +import spray.json._ import ErrorsResponse.ResponseError -import xyz.driver.pdsuicommon.auth.{AnonymousRequestContext, RequestId} -import xyz.driver.pdsuicommon.utils.Utils -import play.api.libs.functional.syntax._ -import play.api.libs.json._ -import play.api.mvc.Results -import xyz.driver.pdsuicommon.validation.JsonValidationErrors +import xyz.driver.pdsuicommon.auth.RequestId final case class ErrorsResponse(errors: Seq[ResponseError], requestId: RequestId) object ErrorsResponse { + import DefaultJsonProtocol._ /** * @param data Any data that can be associated with particular error.Ex.: error field name @@ -21,63 +16,38 @@ object ErrorsResponse { * * @see https://driverinc.atlassian.net/wiki/display/RA/REST+API+Specification#RESTAPISpecification-HTTPStatuscodes */ - final case class ResponseError(data: Option[String], message: String, code: ErrorCode) + final case class ResponseError(data: Option[String], message: String, code: Int) object ResponseError { - implicit val responseErrorJsonFormat: Format[ResponseError] = ( - (JsPath \ "data").formatNullable[String] and - (JsPath \ "message").format[String] and - (JsPath \ "code").format[ErrorCode] - )(ResponseError.apply, unlift(ResponseError.unapply)) + implicit val responseErrorJsonFormat: RootJsonFormat[ResponseError] = jsonFormat3(ResponseError.apply) } - implicit val errorsResponseJsonFormat: Format[ErrorsResponse] = ( - (JsPath \ "errors").format[Seq[ResponseError]] and - (JsPath \ "requestId").format[String] - )((errs, req) => ErrorsResponse.apply(errs, RequestId(req)), res => (res.errors, res.requestId.value)) + implicit val errorsResponseJsonFormat: RootJsonFormat[ErrorsResponse] = new RootJsonFormat[ErrorsResponse] { + override def write(obj: ErrorsResponse): JsValue = { + JsObject( + "errors" -> obj.errors.map(_.toJson).toJson, + "requestId" -> obj.requestId.value.toJson + ) + } - // deprecated, will be removed in REP-436 - def fromString(message: String, httpStatus: Results#Status)( - implicit context: AnonymousRequestContext): ErrorsResponse = { - new ErrorsResponse( - errors = Seq( - ResponseError( - data = None, - message = message, - code = Unspecified - )), - requestId = context.requestId - ) - } + override def read(json: JsValue) = json match { + case JsObject(fields) => + val errors = fields + .get("errors") + .map(_.convertTo[Seq[ResponseError]]) + .getOrElse(deserializationError(s"ErrorsResponse json object does not contain `errors` field: $json")) - // scalastyle:off null - def fromExceptionMessage(e: Throwable, httpStatus: Results#Status = Results.InternalServerError)( - implicit context: AnonymousRequestContext): ErrorsResponse = { - val message = if (e.getMessage == null || e.getMessage.isEmpty) { - Utils.getClassSimpleName(e.getClass) - } else { - e.getMessage - } + val requestId = fields + .get("requestId") + .map(id => RequestId(id.convertTo[String])) + .getOrElse(deserializationError(s"ErrorsResponse json object does not contain `requestId` field: $json")) - fromString(message, httpStatus) - } - // scalastyle:on null + ErrorsResponse(errors, requestId) - // deprecated, will be removed in REP-436 - def fromJsonValidationErrors(validationErrors: JsonValidationErrors)( - implicit context: AnonymousRequestContext): ErrorsResponse = { - val errors = validationErrors.map { - case (path, xs) => - ResponseError( - data = Some(path.toString()), - message = xs.map(_.message).mkString("\n"), - code = Unspecified - ) + case _ => deserializationError(s"Expected json as ErrorsResponse, but got $json") } - - new ErrorsResponse(errors, context.requestId) } } |