Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utility to convert JsResult to Try #41

Merged
merged 2 commits into from Feb 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions play-json/shared/src/main/scala/JsResult.scala
Expand Up @@ -197,8 +197,33 @@ sealed trait JsResult[+A] { self =>
}

object JsResult {
import scala.util.{ Failure, Try, Success }
import play.api.libs.functional._

case class Exception(cause: JsError)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this should be in JsError? i.e. JsError.Exception is an exception produced from a JsError.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

extends java.lang.Exception(Json stringify JsError.toJson(cause))
with scala.util.control.NoStackTrace

/**
* Returns a JSON validation as a [[scala.util.Try]].
*
* @tparam T the type for the parsing
* @param result the JSON validation result
* @param err the function to be applied if the results is an error
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/results/result/

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

*
* {{{
* import scala.concurrent.Future
* import play.api.libs.json.JsResult
*
* def toFuture[T](res: JsResult[T]): Future[T] =
* Future.fromTry(JsResult.toTry(res))
* }}}
*/
def toTry[T](result: JsResult[T], err: JsError => Throwable = Exception(_)): Try[T] = result match {
case e @ JsError(_) => Failure(err(e))
case s @ JsSuccess(v, _) => Success(v)
}

implicit def alternativeJsResult(implicit a: Applicative[JsResult]): Alternative[JsResult] = new Alternative[JsResult] {
val app = a
def |[A, B >: A](alt1: JsResult[A], alt2: JsResult[B]): JsResult[B] = (alt1, alt2) match {
Expand Down
11 changes: 11 additions & 0 deletions play-json/shared/src/test/scala/JsResultSpec.scala
Expand Up @@ -3,6 +3,8 @@
*/
package play.api.libs.json

import scala.util.{ Failure, Success }

import play.api.libs.functional.Functor

import JsResult.functorJsResult
Expand All @@ -19,5 +21,14 @@ class JsResultSpec extends WordSpec with MustMatchers {
JsSuccess(List('j', 's', 'S', 't', 'r'))
)
}

"be converted to Success" in {
JsResult.toTry(JsSuccess("foo")) mustEqual Success("foo")
}

"be converted to basic Failure" in {
val err = JsError("bar")
JsResult.toTry(err) mustEqual Failure(JsResult.Exception(err))
}
}
}