natefaubion / purescript-run

An extensible-effects implementation
MIT License
158 stars 14 forks source link

implement `catchDynE` to catch errors without losing the other effects state #32

Open srghma opened 3 years ago

srghma commented 3 years ago

I didnt find this function in https://hackage.haskell.org/package/freer

but it was in a paper "Freer Monads, More Extensible Effects" http://okmij.org/ftp/Haskell/extensible/more.pdf

it allows catching IO exceptions in the presence of other effects

2020-11-26-18:32:40-screenshot


~current behaviour is~

main = do
  (result :: Either Error Unit) <- Run.runBaseEffect $ runExcept $ do
    Run.catch (errorHander) (Run.liftEffect $ throwError $ error "asdf")
  traceM { result }

~will throw runtime error "asdf" and the { result } is not printed~

~expected: replace Run.catch with Run.catchDynE and catch the js error, print { result } and finish without error code~

srghma commented 3 years ago

another example

output <- runEffect $ runWriter "" do
  write "1"
  catchDynE (\e -> write "error catched") do
    write "2"
    liftEffect $ throw $ error "myerror"
    write "3"
  write "4"

output `shouldEqual` "12error catched4"
natefaubion commented 3 years ago

I think some kind of function like this is useful, but what do you expect the type signature to be? Exceptions in PureScript and exceptions in Haskell are not the same.

natefaubion commented 3 years ago

Is this better handled at the lift layer?

liftAff' = rethrow <=< liftAff <<< attempt