Catcher

class Catcher(partial: PartialFunction[Throwable, Boolean])

Convenience class for extractors that match and return Throwables based on a type and Boolean condition.

Class Catcher was motivated by the need to catch and handle exceptions based on more than just the exception's type as a strategy for dealing with "flickering" tests—tests that usually pass, but occasionally fail. The best strategy for dealing with flickers is to fix the test such that they stop flickering, but sometimes that is not practical. In such cases allowing the test to continue flickering can distract the team by requiring them to spend time inspecting failures to determine whether or not they are flickers or real failures that need attention. Worse, with enough flickers, team members can stop checking all failures and not notice real ones.

One strategy for dealing with flickers you can't practically fix is to catch exceptions that are causing individual flickers and cancel the test when you detect them. Often this means you will need to insert a catch clause in a particular spot, or a pattern match if in a withFixture, looking for a particular exception with a particular message or other identifying attribute. If the same problem is causing flickers in many places, it is handy to create an extractor to detect the problem. This Catcher class provides a factory method that takes a partial function from Throwable to Boolean and produces such an extractor. Here's an example:

val InternalServerError =
 Catcher { case e: DBAccessException =>
   e.getMessage == "500:Internal Server Error"
 }

Using this Catcher in a ScalaTest withFixture method would look like:

override def withFixture(test: NoArgTest) = {
 super.withFixture(test) match {
    case Failed(InternalServerError(ex)) =>
      Canceled("Canceled because likely a flicker caused by intermittently flaky DB", ex)
    case other => other
 }
}
Value parameters:
partial

the partial function that is used by this extractor to determine matches

Companion:
object
Source:
Catcher.scala
class Object
trait Matchable
class Any

Value members

Concrete methods

def unapply(exception: Throwable): Option[Throwable]

Extractor for Throwable that determines matches using on the partial function passed to the constructor.

Extractor for Throwable that determines matches using on the partial function passed to the constructor.

Value parameters:
exception

the exception on which to match

Source:
Catcher.scala