Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding ability to convert play responses and parsers.
- Loading branch information
Flavian Alexandru
committed
Mar 15, 2016
1 parent
54ce42a
commit 1ad5a48
Showing
7 changed files
with
151 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
util-domain/src/main/scala/com/websudos/util/domain/ApiError.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.websudos.util.domain | ||
|
||
case class ApiErrorResponse( | ||
code: Int, | ||
messages: List[String] | ||
) | ||
|
||
case class ApiError(error: ApiErrorResponse) | ||
|
||
object ApiError { | ||
def fromArgs(code: Int, messages: List[String]): ApiError = { | ||
ApiError(ApiErrorResponse(code, messages)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
util-play/src/main/scala/com/websudos/util/play/package.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package com.websudos.util | ||
|
||
import _root_.play.api.data.validation.ValidationError | ||
import _root_.play.api.libs.json.{JsValue, JsPath, Json} | ||
import _root_.play.api.mvc.{Results, Result} | ||
import com.websudos.util.domain.{ApiErrorResponse, ApiError} | ||
|
||
import scala.concurrent.Future | ||
import scala.util.control.NoStackTrace | ||
import scalaz.NonEmptyList | ||
|
||
package object play { | ||
|
||
protected[this] final val defaultErrorCode = 400 | ||
|
||
implicit val apiErrorResponseFormat = Json.format[ApiErrorResponse] | ||
implicit val apiErrorFormat = Json.format[ApiError] | ||
|
||
implicit class NelAugmenter(val list: NonEmptyList[String]) extends AnyVal { | ||
|
||
def response: Result = { | ||
Results.BadRequest(Json.toJson(ApiError(ApiErrorResponse(defaultErrorCode, list.list)))) | ||
} | ||
|
||
def futureResponse(): Future[Result] = { | ||
Future.successful(response) | ||
} | ||
} | ||
|
||
implicit class ParseErrorAugmenter(val errors: Seq[(JsPath, Seq[ValidationError])]) extends AnyVal { | ||
|
||
def errorMessages: List[String] = errors.toList.map { | ||
case (path, validations) => { | ||
s"${path.toJsonString} -> ${validations.map(_.message).mkString(", ")}" | ||
} | ||
} | ||
|
||
def asException: Exception with NoStackTrace = { | ||
new RuntimeException(errorMessages.mkString(", ")) with NoStackTrace | ||
} | ||
|
||
/** | ||
* This will transform a list of accumulated errors to a JSON body that's usable as a response format. | ||
* From a non empty liust of errors this will produce something in the following format: | ||
* | ||
* {{{ | ||
* { | ||
* "error": { | ||
* "code": 400, | ||
* "messages": [ | ||
* "this is an error message", | ||
* "this is another error message | ||
* ] | ||
* } | ||
* } | ||
* }}} | ||
* | ||
* @return | ||
*/ | ||
def apiError: ApiError = ApiError.fromArgs(defaultErrorCode, errorMessages) | ||
|
||
def toJson: JsValue = Json.toJson(apiError) | ||
|
||
def response: Result = Results.BadRequest(toJson) | ||
} | ||
|
||
def errorResponse(msg: String, code: Int = defaultErrorCode): Result = { | ||
Results.BadRequest(Json.toJson(ApiError.fromArgs(code, List(msg)))) | ||
} | ||
|
||
implicit class ResultAugmenter(val res: Result) { | ||
def future: Future[Result] = { | ||
Future.successful(res) | ||
} | ||
} | ||
|
||
def malformedJson(): Result = { | ||
errorResponse("Malformed or missing JSON body") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters