Type class for uniting unrelated failure cases in my Scalaz disjunctions

113 Views Asked by At

I have a for comprehension over Scalaz disjunctions. The left types on these can be different types of error case classes from other libraries. For example, one failure case can be due to an HTTP timeout while another can represent a Json parsing error in Play.

Is there a way of using some form of type class to declare 'these 4 classes/traits are all of this type of error' without actually having them inherit from a common trait? If this does exist, it would also need to be able to be inferred within a for comprehension.

1

There are 1 best solutions below

0
On

I'm new to scalaz, but this is how I'd have handled the issue:

Define our decorators

sealed trait RequestError
case class ParseError(e: PlayJsonError) extends RequestError
case class HttpTimeoutError(e: NettyHttpError) extends RequestError

Use leftMap

val r: RequestError \/ Message = for{
 a <- readHttpContent().leftMap(e => HttpTimeoutError(e))
 b <- parse(a).leftMap(e => ParseError(e))
} yield(b)