Browse files

Leveled-up .validated error reporting.

  • Loading branch information...
1 parent 70488be commit ccd48da270f250e8c6e14ffb3d5af0f0fcf74abd @nuttycom nuttycom committed Jan 2, 2013
View
2 bkka/src/main/scala/blueeyes/bkka/FutureTypeClasses.scala
@@ -6,7 +6,7 @@ import akka.dispatch.ExecutionContext
import scalaz._
class FutureMonad(context: ExecutionContext) extends Applicative[Future] with Monad[Future] {
- def point[A](a: => A) = Future(a)(context)
+ def point[A](a: => A) = Future(a)(context)
def bind[A, B](fut: Future[A])(f: A => Future[B]) = fut.flatMap(f)
override def ap[A,B](fa: => Future[A])(ff: => Future[A => B]) = fa.flatMap{ a => ff.map{ f => f(a) } }
}
View
1 demo/src/main/scala/blueeyes/demo/Data.scala
@@ -49,6 +49,7 @@ package blueeyes.demo {
def validated(jvalue: JValue) = Success(extract(jvalue))
}
}
+
object Extractors extends Extractors
trait Decomposers {
View
2 json/src/main/scala/blueeyes/json/serialization/DefaultExtractors.scala
@@ -171,7 +171,7 @@ trait DefaultExtractors {
}
implicit def StringMapExtractor[V](implicit valueExtractor: Extractor[V]): Extractor[Map[String, V]] = new Extractor[Map[String, V]] {
- private val ev = (valueExtractor.validated _).second[String]
+ private val ev = (valueExtractor.validated(_:JValue)).second[String]
def validated(jvalue: JValue) = jvalue match {
case JObject(fields) =>
View
6 json/src/main/scala/blueeyes/json/serialization/IsoSerialization.scala
@@ -140,7 +140,7 @@ object IsoSerialization {
new ExtractorAux[String :: FT, H :: T] {
def extract(source: JValue, fields: String :: FT) =
for {
- h <- eh.validated(source \ fields.head)
+ h <- eh.validated(source, fields.head)
t <- et.extract(source, fields.tail)
} yield h :: t
}
@@ -152,7 +152,7 @@ object IsoSerialization {
h <- fields.head.alts.find { alt =>
(source \? alt).isDefined
}.map { alt =>
- eh.validated(source \ alt)
+ eh.validated(source, alt)
}.getOrElse(Failure(Invalid("Missing field")))
t <- et.extract(source, fields.tail)
} yield h :: t
@@ -165,7 +165,7 @@ object IsoSerialization {
h <- fields.head.alts.find { alt =>
(source \? alt).isDefined
}.map { alt =>
- eh.validated(source \ alt)
+ eh.validated(source, alt)
}.getOrElse(Success(fields.head.default))
t <- et.extract(source, fields.tail)
} yield h :: t
View
8 json/src/main/scala/blueeyes/json/serialization/Serialization.scala
@@ -18,6 +18,8 @@ trait Extractor[A] { self =>
}
def validated(jvalue: JValue): Validation[Error, A]
+ def validated(jvalue: JValue, jpath: JPath): Validation[Error, A] =
+ ((cause: Extractor.Error) => Extractor.Invalid("Unable to deserialize property or child " + jpath, Some(cause))) <-: validated(jvalue.get(jpath))
def project(jpath: JPath): Extractor[A] = new Extractor[A] {
override def extract(jvalue: JValue) = self.extract(jvalue(jpath))
@@ -58,7 +60,9 @@ object Extractor {
}
}
- case class Invalid(message: String) extends Error
+ case class Invalid(msg: String, cause: Option[Error] = None) extends Error {
+ def message = cause map { c => "%s (caused by %s)".format(msg, c.message) } getOrElse msg
+ }
case class Thrown(exception: Throwable) extends Error {
def message: String = exception.getMessage
@@ -111,6 +115,7 @@ trait SerializationImplicits {
case class DeserializableJValue(jvalue: JValue) {
def deserialize[T](implicit e: Extractor[T]): T = e.extract(jvalue)
def validated[T](implicit e: Extractor[T]): Validation[Extractor.Error, T] = e.validated(jvalue)
+ def validated[T](jpath: JPath)(implicit e: Extractor[T]) = e.validated(jvalue, jpath)
}
case class SerializableTValue[T](tvalue: T) {
@@ -122,6 +127,7 @@ trait SerializationImplicits {
implicit def TValueToJValue[T](tvalue: T): SerializableTValue[T] = SerializableTValue[T](tvalue)
}
+
object SerializationImplicits extends SerializationImplicits
/** Bundles default extractors, default decomposers, and serialization

0 comments on commit ccd48da

Please sign in to comment.