Bohtvaroh

Blogger - FPOWJPWB - Retry in Clojure

Jul 26th, 2012
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (defn retry
  2.   [op checker & {:keys [pre-retry times error-message]
  3.                  :or {pre-retry #(), times 1, error-message "Retries exhausted"}}]
  4.   (let [[result e] (try [(op) nil] (catch Exception e [nil e]))]
  5.     (if (and (nil? e) (checker result))
  6.       result
  7.       (if (pos? times)
  8.         (do (pre-retry)
  9.             (recur op checker {:pre-retry pre-retry
  10.                                :times (dec times)
  11.                                :error-message error-message}))
  12.         (or e (Exception. error-message))))))
Advertisement
Add Comment
Please, Sign In to add comment