Skip to content

Commit

Permalink
docs: Add minimal error handling to tutorial code sample
Browse files Browse the repository at this point in the history
The code will crash a lot without this error handling.
(Especially when editing the CSV file in Emacs)
  • Loading branch information
rvl committed Aug 8, 2024
1 parent 0d268f8 commit 2f6015f
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions docs/tutorial/03-dynamic.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,15 @@ In the case of our mood tracker, we will use the [fsnotify](https://hackage.hask
logInfoNS "fsnotify" "Waiting for fs event ..."
evt <- liftIO $ readChan ch
logInfoNS "fsnotify" $ "Got fs event: " <> show evt
setModel =<< readModel (FSNotify.eventPath evt)
handle (\(e :: IOException) -> logErrorNS "fsnotify" (show e)) $
setModel =<< readModel (FSNotify.eventPath evt)
loop
loop
where
readModel fp = do
s <- readFileLBS fp
case toList <$> Csv.decode Csv.NoHeader s of
Left err -> throw $ userError err
Left err -> logErrorNS "csv" (toText err) >> pure (Model mempty)
Right moods -> pure $ Model $ Map.fromList moods
-- Observe changes to a directory path, and return the `Chan` of its events.
watchDirForked :: FilePath -> IO (Chan FSNotify.Event)
Expand Down

0 comments on commit 2f6015f

Please sign in to comment.