Hi guys, I'm having some trouble with the pause-on-error middleware combined with fun-mode, specifically after an error happens and you fix the error, you should be able to unpause and continue the sketch.
But if something throws in the update function, it seems that state becomes nil, so after you continue the sketch you'll continue to get infinite errors. Any ideas/ways around this?
I managed to fix it. The problem is that the pause middleware is not fun-mode aware, it nullifies the state when there is an error and also unpause. I rewrote the pause middleware like this:
(ns muddy.pause-on-error
(:require [quil.core :as q]
[clojure.string :as cstr]))
(defn- wrap-fn [pause name function do-on-pause]
(fn [& args]
(if @pause
(do
(do-on-pause)
(first args))
(try
(apply function args)
(catch Exception e
(println "Exception in " name " function: " e)
(reset! pause {:name name})
(first args))))))
(defn- draw-error-message [pause]
(let [^processing.core.PGraphics error (q/create-graphics (q/width) (q/height))]
(q/with-graphics error
(q/push-style)
(q/background 255)
(q/fill 0)
(q/text-size 15)
(let [{:keys [name]} @pause
str [(format "Sketch was paused due to an exception thrown in %s" name)
(str "Fix the error and then press any key to unpause sketch.")]]
(q/text (cstr/join \newline str) 10 20))
(q/pop-style))
(q/set-image 0 0 error)))
(defn- unpause [pause]
(println "Unpausing sketch")
(reset! pause nil))
(defn pause-on-error
"Pauses the sketch if any of the user-provided handlers throws an error."
[options]
(let [pause (atom nil)
update (:update options)
draw (:draw options)
key-pressed (:key-pressed options)]
(into options {:update (wrap-fn pause :update update (fn []))
:draw (wrap-fn pause :draw draw #(draw-error-message pause))
:key-pressed (wrap-fn pause :key-pressed key-pressed #(unpause pause))})))