singpolyma / unexceptionalio

IO without any PseudoExceptions
http://hackage.haskell.org/package/unexceptionalio
ISC License
28 stars 5 forks source link

Possibly implement a funtion to exit for PsuedoExceptions #21

Open bbarker opened 3 years ago

bbarker commented 3 years ago

I recently had (possibly good) cause to have a function like this:

    seOrExit ::  SX.SomeException -> SomeNonPseudoException
    seOrExit e = case SX.fromException e of
      Just se -> se
      Nothing ->
        unsafePerformIO $ SE.die  "Received a PseudoException, exiting! "

The context is that I can use seOrExit with a mapError that works on my monad transformer stack that is built on top of UIO. If you're chugging along, and get a psuedo exception, I'm not sure what else there is to be done about it anyway, other than performing an alternative form of logging.

Anyway, wanted to get your thoughts on this use case. Maybe this is terrible.

singpolyma commented 3 years ago

If you use unexceptionalid functions to build a stack based on UIO I'm curious how you end up with a SomeException at all? Should always end up with at most a SomeNonPseudoException because PseudoException should not be caught in UIO context.

bbarker commented 3 years ago

Perhaps I went wrong somewhere, but here's what I did:

  xsd <- liftEither $ parseText def $ TL.fromStrict xsdTxt

Due to parseText (being a function from a 3rd party library) possibly returning Left SomeException as part of it's signature, I then needed a function like mapZErrorOrExit :: ZIO r SX.SomeException a -> ZIO r SomeNonPseudoException a to absolve the error as in:

  xsd <- mapZErrorOrExit $ liftEither $ parseText def $ TL.fromStrict xsdTxt

I can imagine other alternatives, though I haven't really imagined any obviously better alternatives so far.

singpolyma commented 3 years ago

Oh interesting. That's a case I guess I hadn't considered: when a library already catches/returns exceptions but doesn't use anything more specific than SomeException.

Here's an option:

xsd <- UIO.fromIO $ UIO.runEitherIO $ return $ parseText def $ TL.fromStrict xsdTxt