owickstrom / gi-gtk-declarative

Declarative GTK+ programming in Haskell
https://owickstrom.github.io/gi-gtk-declarative/
288 stars 35 forks source link

Ignored exception in update handler #77

Closed guibou closed 4 years ago

guibou commented 4 years ago

The following code sample exhibits a scenario where exceptions are ignored and break event handling.

{-# LANGUAGE OverloadedLabels #-}
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
{-# LANGUAGE TemplateHaskell #-}

import qualified GI.Gtk as Gtk
import GI.Gtk.Declarative (Attribute (..), bin, container, on, widget)
import GI.Gtk.Declarative.App.Simple (App (..), AppView, Transition (..), run)
import qualified Data.Text as Text
import Control.Monad (void)

data AppEvent = Inc | Inc2

view' ::
  Int ->
  AppView Gtk.Window AppEvent
view' i =
  bin
    Gtk.Window
    [ #title := "Tablet UI"
    ]
    $ container
      Gtk.Box
      []
      [ widget
          Gtk.Label
          [#label := (Text.pack $ show i)],
        widget
          Gtk.Button
           [#label := "Click",
            on #clicked Inc
           ],
        widget
          Gtk.Button
           [#label := "Click 2",
            on #clicked Inc2
           ]
      ]

update' ::
  Int ->
  AppEvent ->
  Transition Int AppEvent
update' i Inc = Transition (i+1) (pure Nothing)
update' i Inc2 = error "die"

main :: IO ()
main = do

  void $ run $ App
    { update = update',
      view = view',
      inputs = [],
      initialState = 0
    }

It generates a label and two click buttons. The first one is generating the Inc event and the second one is generating the Inc2 event.

Unfortunately, the update' function raises an exception for the Inc2 event.

I was expecting a runtime exception when clicking on the second button, but instead, the event handling is now broken and all of that happen silently. Clicking on the first button is working until we click on the second button, and then it does not work anymore.

Is it possible to have a fatal exception instead, or at least a message in the console such as "An exception occurred in the event handler: blablabla" if you think that the event handler must recover from exceptions.

Thank you.