unexceptionalio icon indicating copy to clipboard operation
unexceptionalio copied to clipboard

Possibly implement a funtion to exit for PsuedoExceptions

Open bbarker opened this issue 4 years ago • 3 comments
trafficstars

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.

bbarker avatar Dec 17 '20 20:12 bbarker

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.

singpolyma avatar Dec 17 '20 20:12 singpolyma

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.

bbarker avatar Dec 17 '20 21:12 bbarker

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

singpolyma avatar Dec 18 '20 02:12 singpolyma