kowainik / eio

🎯 IO with Exceptions tracked on the type-level
Mozilla Public License 2.0
58 stars 2 forks source link

[#7] Custom Errors for runEIO #13

Open JonathanLorimer opened 3 years ago

JonathanLorimer commented 3 years ago

This haskell code generates the corresponding type error:

data MyErr1 = MyErr1 deriving (Show)
instance Exception MyErr1

data MyErr2 = MyErr2 deriving (Show)
instance Exception MyErr2

data MyErr3 = MyErr3 deriving (Show)
instance Exception MyErr3

safeMainWrong :: IO ()
safeMainWrong = runEIO errorsAbound
  where
    errorsAbound = (throw MyErr1) EIO.>> (throw MyErr2) EIO.>> (throw MyErr3)
rc/EIO.hs:96:17: error:
    • The 'runEIO' handler requires that all exceptions in 'EIO' to be handled.
      The action 'runEIO' is applied to throws the following unhandled exceptions:
        • MyErr1
        • MyErr2
        • MyErr3

    • In the expression: runEIO errorsAbound
      In an equation for ‘safeMainWrong’:
          safeMainWrong
            = runEIO errorsAbound
            where
                errorsAbound
                  = (throw MyErr1) EIO.>> (throw MyErr2) EIO.>> (throw MyErr3)
   |
96 | safeMainWrong = runEIO errorsAbound
   |                 ^^^^^^^^^^^^^^^^^^^
JonathanLorimer commented 3 years ago

Whoops, this isn't actually ready for review, I added DocTests and found a bug where my type family for a custom error message can't resolve EIO (e1 <> e2) a. I need to look into this more!

JonathanLorimer commented 3 years ago

Okay, sorted out the funny type error, I was using the EIO constructor to lift an IO action, but I really need an unsafeLiftIO which allows you to run an IO action in EIO as long as you promise it doesn't throw an exception.