Fork me on GitHub
#cljfx
<
2020-09-10
>
Grigory Shepelev08:09:04

A little question

Grigory Shepelev08:09:56

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"})))}))

Grigory Shepelev08:09:21

And thank you @vlaaad for such a nice lib.

Grigory Shepelev08:09:53

But the code above doesn't work as expected. When exception happends catch block not evaluated at all.

vlaaad08:09:54

how do you know it’s not evaluated? have you tried putting println in the catch block?

vlaaad08:09:45

looking at your renderer I would say it should be evaluated

vlaaad08:09:01

Perhaps the exception happens in some async thread?

Grigory Shepelev08:09:47

I will send a full code to try a little later. Sorry, I have to go now

Grigory Shepelev08:09:19

At least I know I've got the idea right

vlaaad08:09:35

yeah, I think you are on the right track!

Grigory Shepelev09:09:45

(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"))))}))

Grigory Shepelev09:09:49

(fx/mount-renderer *state renderer)

vlaaad09:09:48

print->println

vlaaad10:09:15

print does not flush the output, so you won’t see results immediately

Grigory Shepelev10:09:32

still nothing 😞

vlaaad10:09:18

have you called (fx/mount-renderer *state renderer) again after redefining renderer to use println instead of print ?

vlaaad10:09:22

it worked for me

Grigory Shepelev10:09:34

Oh. I'm running it in calva repl... I guess that's the problem

Grigory Shepelev10:09:22

I'm really sorrt.

Grigory Shepelev10:09:30

sorry. It's the repl situation

vlaaad10:09:58

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

vlaaad10:09:15

it might be in some terminal window that calva started

Grigory Shepelev10:09:46

I figured that out. Thanks

vlaaad10:09:18

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)