How to Recover on a Failed Task? - scalaz

Given:
import scalaz.concurrent.Task
import scalaz._
val result: Task[String \/ Int] = Task { throw new RuntimeException("!")}
Then, I ran it to get a RuntimeException:
scala> result.unsafePerformSync
java.lang.RuntimeException: !
...
Does Task have an "equivalent" Future#recover, i.e. something like the following?
def recover[A, B, C](t: Task[A], handleError: Throwable => B \/ C]: Task[B \/ C]

Task#handle (or alternatively handleWith) does the trick
def f: String = {
throw new RuntimeException()
"I will fail"
}
Task(f).handle{ case _ ⇒ "recovered"}.run \\ recovered

Related

Play JSON serialize/deserialize

I'm trying to go create some formats for this:
case class Work[T](todo: Seq[T], failed: Seq[T], success: Seq[T])
object Work {
implicit def format[T](implicit r: Reads[T], w: Writes[T]): Format[Work[T]] = Json.format[Work[T]]
}
object InternalMessage {
implicit def format[D, R](implicit
rD: Reads[D],
wD: Writes[D],
rR: Reads[R],
wR: Writes[R]
): Format[InternalMessage[D, R]] = Json.format[InternalMessage[D, R]]
}
case class InternalMessage[D, R](
download: List[Work[D]],
refine: List[Work[R]],
numberOfTries: Int
)
This doesn't work and I don't understand why. The error is
[error] /home/oleber/develop/data-platform/modules/importerTemplate/src/main/scala/template/TemplateModel.scala:46: No apply function found matching unapply parameters
[error] implicit def format[T](implicit r: Reads[T], w: Writes[T]): Format[Work[T]] = Json.format[Work[T]]
[error] ^
[error] /home/oleber/develop/data-platform/modules/importerTemplate/src/main/scala/template/TemplateModel.scala:55: No apply function found matching unapply parameters
[error] ): Format[InternalMessage[D, R]] = Json.format[InternalMessage[D, R]]
Thanks for any help
AFAIU you can't generate JSON Format for such data types using Play 2.5 macros. What is limiting you is that you use Seq[T] i.e. you want JSON serialization for a generic class that has fields that use some complex type built from that generic type rather than just raw generic T. Relevant code seems to be in the JsMacroImpl lines 135-141. In those lines inside paramsMatch inner method of maybeApply calculation the macro checks if there are an apply and unapply methods with matching (complement) signatures:
val maybeApply = applies.collectFirst {
case (apply: MethodSymbol) if hasVarArgs && {
// Option[List[c.universe.Type]]
val someApplyTypes = apply.paramLists.headOption.map(_.map(_.asTerm.typeSignature))
val someInitApply = someApplyTypes.map(_.init)
val someApplyLast = someApplyTypes.map(_.last)
val someInitUnapply = unapplyReturnTypes.map(_.init)
val someUnapplyLast = unapplyReturnTypes.map(_.last)
val initsMatch = someInitApply == someInitUnapply
val lastMatch = (for {
lastApply <- someApplyLast
lastUnapply <- someUnapplyLast
} yield lastApply <:< lastUnapply).getOrElse(false)
initsMatch && lastMatch
} => apply
case (apply: MethodSymbol) if {
val applyParams = apply.paramLists.headOption.
toList.flatten.map(_.typeSignature)
val unapplyParams = unapplyReturnTypes.toList.flatten
def paramsMatch = (applyParams, unapplyParams).zipped.forall {
case (TypeRef(NoPrefix, applyParam, _),
TypeRef(NoPrefix, unapplyParam, _)) => // for generic parameter
applyParam.fullName == unapplyParam.fullName
case (applyParam, unapplyParam) => applyParam =:= unapplyParam
}
(applyParams.size == unapplyParams.size && paramsMatch)
} => apply
}
As you can see in the relevant (non-hasVarArgs branch), this code only handles two cases: when the types in apply and unapply match exactly or when the same raw generic type is used. Your case of Seq[T] (as well as other complex generic types) is not handled here and the error you get is generated just a few lines down when maybeApply is empty:
val (tparams, params) = maybeApply match {
case Some(apply) => {
apply.typeParams -> apply.paramLists.head
// assume there is a single parameter group
}
case None => c.abort(c.enclosingPosition, "No apply function found matching unapply parameters")
}
In Play-JSON 2.6 this code was significantly re-worked and now it seems to support your case as well (see conforms inner method).
If upgrading to (not released yet) Play-JSON 2.6 is not acceptable, you can create Formats object(s) yourself using JSON Reads/Writes/Format Combinators doc. Something like this should work for you:
implicit def format[T](implicit r: Reads[T], w: Writes[T]): Format[Work[T]] = {
val workReads = (
(JsPath \ "todo").read[Seq[T]] and
(JsPath \ "failed").read[Seq[T]] and
(JsPath \ "success").read[Seq[T]]
) (Work.apply[T] _)
val workWrites = (
(JsPath \ "todo").write[Seq[T]] and
(JsPath \ "failed").write[Seq[T]] and
(JsPath \ "success").write[Seq[T]]
) (unlift(Work.unapply[T]))
new Format[Work[T]] {
override def reads(json: JsValue): JsResult[Work[T]] = workReads.reads(json)
override def writes(o: Work[T]): JsValue = workWrites.writes(o)
}
}

Is JSON formatting not enough for parsing case class having > 22 fields?

My case class has 30 fields. For simplicity, I use 4 fields,
case class Person(id: Long, name: String, age: Int, sex: Sex)
val personFormat1: OFormat[(Long, String)] = ((__ \ "id").format[Long] ~ (__ \ "name").format[String]).tupled
val personFormat2: OFormat[(Int, Sex)] = ((__ \ "age").format[Int] ~ (__ \ "sex").format[Sex]).tupled
implicit val personFormat: Format[Person] = (personFormat1 ~ personFormat2)({
case ((id, name), (age, sex)) => new Person(id, name, age, sex)
}, (person: Format) => ((person.id, person.name), (person.age, person.sex)))
But even after writing formatter with format1 as a group of 22 fields and format2 as a group of 8 fields, I get error when am trying to parse the json of this case class.
Error is
No Json serializer as JsObject found for type Person. Try to implement an implicit OWrites or OFormat for this type.
How to write implicit Owrites or OFormat? or how to fix this issue?
I use Play-Json extensions library for working with JSON with more than 22 fields: https://github.com/xdotai/play-json-extensions
libraryDependencies += "ai.x" %% "play-json-extensions" % "0.8.0"
You need to an implicit writer to do this. Something like this
implicit val person= Json.format[Person]
Also, if you are using custom data types, like for your case Sex you need to specify a reader and writer. You do not need to do this for primitive types like Int, Long, String Etc.
def enumReads[T <: Enum[T]](mkEnum: String => T) = new Reads[T] {
override def reads(json: JsValue): JsResult[T] =
json match {
case JsString(s) => try {
JsSuccess(mkEnum(s))
}
catch {
case e: IllegalArgumentException =>
JsError("Not a valid enum value: " + s)
}
case v => JsError("Can't convert to enum: " + v)
}
}
implicit val enumWrites = new Writes[Enum[_]] {
def writes(e: Enum[_]) = JsString(e.toString)
}
implicit val sex = enumReads(Sex.valueOf)
Also, upgrade to scala 2.11 or later to avoid the limitation of 22 fields in case class. For more info see here: How to get around the Scala case class limit of 22 fields?
I found this using google, and this worked greatly for me
// https://mvnrepository.com/artifact/com.chuusai/shapeless_2.11
libraryDependencies += "com.chuusai" % "shapeless_2.11" % "2.3.2"
// https://mvnrepository.com/artifact/org.julienrf/play-json-derived-codecs_2.11
libraryDependencies += "org.julienrf" % "play-json-derived-codecs_2.11" % "3.2"

Reads with validation for email or empty string in Play/Scala

I am trying to create a validator for reads that will only allow a valid email or an empty string.
What I tried until now seem to work only partially.
Here is my reads defenition:
case class EmailFieldValueForm(value: String) extends FieldValueForm
val emailFieldValueFormReads: Reads[EmailFieldValueForm] =
(__ \ "value").read[String](email or maxLength(0)).map(EmailFieldValueForm.apply _)
When I test it, I am getting the following error:
diverging implicit expansion for type play.api.libs.json.Reads[V]
[error] starting with value uuidReads in trait DefaultReads [error]
(__ \ "value").read[String](email or
maxLength(0)).map(EmailFieldValueForm.apply _)
Also, tried doing it with regex:
val emailFieldValueFormReads: Reads[EmailFieldValueForm] =
(__ \ "value").read[String](email or pattern("""^$"""r)).map(EmailFieldValueForm.apply)
And in this case, any json I provide is passing.For example:
val invalidJson = Json.parse(
"""
|{
| "value": "boo"
|}
""".stripMargin
Simply gives me an empty value, but does not fail validation.
What am I doing wrong?
Thanks,
SOLUTION
Seems like I was not doing the testing correctly. The following worked:
val invalidJson = Json.parse(
"""
|{
| "value": "boo"
|}
""".stripMargin
)
(EmailFieldValueForm.emailFieldValueFormReads reads invalidJson match {
case JsSuccess(value, _) => value
case JsError(e) => throw JsResultException(e)
}) must throwA[JsResultException]
The problem lies in implicit reads: Reads[M] for def maxLength[M](m: Int)(implicit reads: Reads[M], p: M => scala.collection.TraversableLike[_, M])
You may specify type M explicitly and all will be fine
import play.api.libs.functional.syntax._
import play.api.libs.json.Reads._
import play.api.libs.json._
val emailFieldValueFormReads: Reads[EmailFieldValueForm] =
((__ \ "value").read[String](email or maxLength[String](0))).map(EmailFieldValueForm.apply _)
Json.parse("""{"value": "sss"}""").validate[EmailFieldValueForm](emailFieldValueFormReads)
Json.parse("""{"value": ""}""").validate[EmailFieldValueForm](emailFieldValueFormReads)
Json.parse("""{"value": "a#a.com"}""").validate[EmailFieldValueForm](emailFieldValueFormReads)
scala> res0: play.api.libs.json.JsResult[EmailFieldValueForm] = JsError(List((/value,List(ValidationError(List(error.email),WrappedArray()), ValidationError(List(error.maxLength),WrappedArray(0))))))
scala> res1: play.api.libs.json.JsResult[EmailFieldValueForm] = JsSuccess(EmailFieldValueForm(),/value)
scala> res2: play.api.libs.json.JsResult[EmailFieldValueForm] = JsSuccess(EmailFieldValueForm(a#a.com),/value)
For testing with scalatest (PlaySpec) you may use
Json.parse("""{"value": "sss"}""").validate[EmailFieldValueForm](emailFieldValueFormReads) must be an 'error
Json.parse("""{"value": "a#a.com"}""").validate[EmailFieldValueForm](emailFieldValueFormReads) must be an 'success
Json.parse("""{"value": ""}""").validate[EmailFieldValueForm](emailFieldValueFormReads) must be an 'success

PlayFramework in Scala, Reads - error in case of unknown key? [duplicate]

Play's JSON serialization is by default permissive when serializing from JSON into a case class. For example.
case class Stuff(name: String, value: Option[Boolean])
implicit val stuffReads: Reads[Stuff] = (
( __ \ 'name).read[String] and
( __ \ 'value).readNullable[Boolean]
)(Stuff.apply _)
If the following JSON was received:
{name: "My Stuff", value: true, extraField: "this shouldn't be here"}
It will succeed with a 'JsSuccess' and discard the 'extraField'.
Is there a way to construct the Json Reads function to have it return a JsError if there are 'unhandled' fields?
You can verify that the object doesn't contain extra keys before performing your own decoding:
import play.api.data.validation.ValidationError
def onlyFields(allowed: String*): Reads[JsObject] = Reads.filter(
ValidationError("One or more extra fields!")
)(_.keys.forall(allowed.contains))
Or if you don't care about error messages (and that one's not very helpful, anyway):
def onlyFields(allowed: String*): Reads[JsObject] =
Reads.verifying(_.keys.forall(allowed.contains))
And then:
implicit val stuffReads: Reads[Stuff] = onlyFields("name", "value") andThen (
(__ \ 'name).read[String] and
(__ \ 'value).readNullable[Boolean]
)(Stuff)
The repetition isn't very nice, but it works.
Inspired from Travis' comment to use LabelledGeneric I was able achieve compile time safe solution.
object toStringName extends Poly1 {
implicit def keyToStrName[A] = at[Symbol with A](_.name)
}
case class Foo(bar: String, boo: Boolean)
val labl = LabelledGeneric[Foo]
val keys = Keys[labl.Repr].apply
now keys.map (toStringName).toList will give you
res0: List[String] = List(bar, boo)

scala json4s handling null values

I have a case class
case class Test(a: String, b: Option[Double])
object TestSerializer extends CustomSerializer[Test] (format => ({
case jv: JValue =>
val a = (jv \ "a").extract[String]
val b = (jv \ "b").extractOpt
Test(a, b)
},{
case tst: Test =>
tst.b match {
case Some(x) => ("a" -> "test") ~ ("b" -> x)
case None => ("a" -> "test") ~ ("b" -> "NA")
}
}))
When b is available, the result I get is: {a: "test", b: 1.0}
When b = None, the result I get is: {a: "test"}
The second result throws an exception in the first partial function since it cannot find b.
How can I ensure my code does not fail and instead treat the missing b value of json as None?
I am using json4s 3.2.10 and not 3.2.11 so I cannot use the preserveEmpty fields option.