Skip to content

Instantly share code, notes, and snippets.

@ElectricCoffee
Created April 3, 2014 08:37
Show Gist options
  • Save ElectricCoffee/9950655 to your computer and use it in GitHub Desktop.
Save ElectricCoffee/9950655 to your computer and use it in GitHub Desktop.
Basic method that lets you attempt to get a result up to a given number of times
def attempt[T,E <: Exception](attempts: Int, subject: String, failedMessage: String, exception: E, failCondition: T => Boolean)(body: => T): T = {
val i = body
if(failCondition(i) && attempts != 0) {
println(s"Subject: $subject")
println(s"Message: $failedMessage")
println(s"Attempts left: $attempts")
// do something with the exception
attempt(attempts - 1, subject, failedMessage, exception, failCondition)(body)
}
else i
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment