Gabriella439 / turtle

Shell programming, Haskell style
BSD 3-Clause "New" or "Revised" License
943 stars 90 forks source link

catch catches error in monad continuation #421

Open StillerHarpo opened 2 years ago

StillerHarpo commented 2 years ago

I have the following code

prog1 :: Shell ()
prog1 = do
  echo "prog1"
  catch
    ( do
        echo "will be catched"
        pure ())
    (\(_ :: SomeException) -> do
        echo "exception")

prog2 :: IO ()
prog2 = do
  echo "prog2"
  procs "lll" [] mempty

mainMenu' :: Shell ()
mainMenu' = do
  echo "mainMenu"
  prog1
  liftIO prog2

I would expect that running this will not run the error handler (it will not print "exception). But runnig it prints it

*Main> sh $ mainMenu'
mainMenu
prog1
will be catched
prog2
exception
prog2
*** Exception: lll: createProcess: posix_spawnp: does not exist (No such file or directory)

Doing the same thing in the IO monad works as I expect

I'm on version 1.5.22

Gabriella439 commented 2 years ago

Yeah, there's not a good way to fix this, short of removing the MonadCatch instance altogether.

What's happening is that whenever you catch something in Shell the exception handler is catching all exceptions that will ever be raised by that Shell, not just exceptions in the wrapped block.