Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object retry {
- def apply[Result](operation: () => Result,
- checker: Result => Boolean,
- preRetry: () => Any = null,
- times: Int = 1,
- errorMessage: String = "Retries exhausted"): Result = {
- val (operationResult, operationException): (Result, Exception) = try {
- (operation(), null)
- } catch {
- case e: Exception => (null, e)
- }
- if (operationException == null && checker(operationResult))
- operationResult
- else {
- if (times > 0) {
- if (preRetry != null) preRetry()
- apply(operation, checker, preRetry, times - 1, errorMessage)
- } else
- throw
- if (operationException != null) operationException else new Exception(errorMessage)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment