A little question
This is my renderer
(def renderer
(fx/create-renderer
:middleware (fx/wrap-map-desc #(root-view %))
:opts {:fx.opt/map-event-handler
(fn [x]
(try
(handle x)
(catch Exception e
{"want to show alert with exception info here"})))}))
And thank you @vlaaad for such a nice lib.
But the code above doesn't work as expected. When exception happends catch block not evaluated at all.
how do you know it’s not evaluated? have you tried putting println
in the catch block?
looking at your renderer I would say it should be evaluated
Yes
I did tried
Perhaps the exception happens in some async thread?
Nope. Linear.
I will send a full code to try a little later. Sorry, I have to go now
sure, np!
At least I know I've got the idea right
yeah, I think you are on the right track!
(ns example.gui
(:require [cljfx.api :as fx]
[clojure.string :as str]))
(def *state
(atom 0))
(defmulti handle ::event)
(defmethod handle ::press
[_]
(throw
(Exception. "error")))
(defn root-view
[a]
{:fx/type :stage
:title "App"
:showing true
:width 800
:height 600
:scene
{:fx/type :scene
:root
{:fx/type :v-box
:alignment :center
:spacing 15
:children
[{:fx/type :h-box
:alignment :center
:spacing 15
:children
[{:fx/type :button
:text "Button"
:on-action {::event ::press}}]}]}}})
(def renderer
(fx/create-renderer
:middleware (fx/wrap-map-desc #(root-view %))
:opts {:fx.opt/map-event-handler
(fn [x]
(try
(handle x)
(catch Exception e
(print "sas"))))}))
(fx/mount-renderer *state renderer)
Try this @vlaaad
print->println
print does not flush the output, so you won’t see results immediately
still nothing 😞
have you called (fx/mount-renderer *state renderer)
again after redefining renderer to use println
instead of print
?
it worked for me
1 min
Oh. I'm running it in calva repl... I guess that's the problem
hmmm
I'm really sorrt.
sorry. It's the repl situation
I don’t know how calva output works, but I would guess that it can show only what was printed in the main thread, while JavaFX event handling happens on the JavaFX UI thread, so output probably goes to your process output pane
it might be in some terminal window that calva started
I figured that out. Thanks
I slightly edited your code so it shows a dialog:
(ns cljfx-ex
(:require [cljfx.api :as fx]
[clojure.main :as m]
[clojure.pprint :as pp]))
(def *state
(atom 0))
(defmulti handle ::event)
(defmethod handle ::press [_] (throw (Exception. "error")))
(defn root-view
[a]
{:fx/type :stage
:title "App"
:showing true
:width 800
:height 600
:scene {:fx/type :scene
:root {:fx/type :v-box
:alignment :center
:spacing 15
:children [{:fx/type :h-box
:alignment :center
:spacing 15
:children [{:fx/type :button
:text "Button"
:on-action {::event ::press}}]}]}}})
(def renderer
(fx/create-renderer
:middleware (fx/wrap-map-desc #(root-view %))
:opts {:fx.opt/map-event-handler
(fn [x]
(try
(handle x)
(catch Exception e
(fx/create-component
{:fx/type :dialog
:showing true
:dialog-pane {:fx/type :dialog-pane
:header-text "Event handling exception"
:content-text (-> e Throwable->map m/ex-triage m/ex-str)
:expandable-content {:fx/type :label
:text (with-out-str (pp/pprint e))}
:button-types [:ok]}}))))}))
(fx/mount-renderer *state renderer)
Yep. Thanks.