Created
November 20, 2016 16:05
-
-
Save firthh/906ab37874d86c288192b48e5032265b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn with-error-handling [f] | |
(fn [& args] | |
(try | |
(if-let [res (apply f args)] | |
[res nil] | |
[nil {:message (str "Had nil returned calling " f " with " args)}] | |
(catch Exception e | |
[nil {:exception e :message (str "Had exception calling " f " with " args)}])))) | |
(defn our-comp | |
"Same as `clojure.core/comp` except that the functions will shortcircuit on nil. | |
((comp inc inc) 1) => 3 | |
((comp inc inc) nil) => nil" | |
[& fs] | |
(fn comp-fn | |
([i] (comp-fn i fs)) | |
([[i e] [f & more]] | |
(cond (nil? i) [nil e] | |
(nil? f) [i e] | |
:else (recur (f i) more))))) | |
(def transform | |
(->> [:data | |
transform-1 | |
transform-2 | |
transform-3 | |
transform-4 | |
transform-5 | |
transform-6 | |
transform-7] | |
(map with-error-handling) | |
(apply new-comp))) | |
(defn process-errors! [[v e]] | |
(when e | |
(let [{:keys [message exception]} e] | |
(when exception (airbrake/notify config/airbrake exception)) | |
(log/error message))) | |
v) | |
(defn process-clicks-messages [db-connection objs] | |
(->> objs | |
(map transform) | |
(map process-errors!) | |
(remove nil?) | |
(run! (partial db/save-objects! db-connection)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment