-- Drew
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
I actually go this route, but with a custom error type:
final case class ONOES(msg: String, err: Option[Throwable])
You can usually make a good string representing what failed, but you don't always have an error.
Then, you need something like Try, but that let's you pick a good error message, like:
Validating("Could not load data") {
...
}
Combined with the ability to run "sanity checks" on data and aggregate errors, you can get a better user experience out of your error reporting.
It's convenient to have a hierarchy of types given that wrapping and unwrapping them gets awkward after a while. Personally, I use, in order of complexity of what can be wrapped,
Option
Try
Either
May
Tri
where the last two are equivalent to Option[Either[A,B]] and Try[Either[A,B]] (with special support for a None-like state, so perhaps actually Try[Option[Either[A,B]]]). Of course you can just build up the pieces by yourself, but Tri encapsulates my error-handling workflow better than anything else I've come up with. (Tri because of three possible states, which I amuse myself by calling Good, Bad, and Ugly--the last of which holds an exception.)
Anyway, I think the real point is "understand your error-handling needs and adopt a structure that satisfies them", which may include Try or Either or Option or all of the above. Try's secret weapon is manipulation of a returned value in a bulletproof way; Option's advantage is its simplicity; Either's is its flexibility. (May merges simplicity for simple cases with flexibility when you need an alternate type, and Tri attempts to merge all three, though I couldn't honestly call it "simple" any more.)
To a considerable extent, what you use is a matter of style, not a matter of what's "better".