Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#7] Custom Errors for runEIO #13

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

JonathanLorimer
Copy link

@JonathanLorimer JonathanLorimer commented Apr 3, 2021

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:MyErr1MyErr2MyErr3In 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 JonathanLorimer changed the title add custom type error [#7] Custom Errors for runEIO Apr 3, 2021
Comment on lines +25 to +33
ghcid.txt

# Stack
.stack-work/
stack.yaml.lock

# Nix
shell.nix

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hope you don't mind me adding these. I like to use nix-shell to pull in all the project dependencies (such as ghc, and cabal) and use ghcid for development. I didn't want to clutter up anyone else's workflow so I added the files I use to .gitignore. If this is annoying I can just stage my files more carefully and remove this.

Comment on lines +17 to +18
( 'Text "The 'runEIO' handler requires that all exceptions in 'EIO' to be handled."
':$$: 'Text "The action 'runEIO' is applied to throws the following unhandled exceptions:"
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to keep the text as similar to your suggestion as possible.

src/EIO.hs Outdated
@@ -62,9 +63,10 @@ safeMain = EIO.do

@since 0.0.0.0
-}
runEIO :: EIO '[] () -> IO ()
runEIO :: DisallowUnhandledExceptions excepts => EIO excepts () -> IO ()
Copy link
Author

@JonathanLorimer JonathanLorimer Apr 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Being new to custom type errors, and type level programming in general, I wasn't sure if there was a cleaner way to convey, to the consumer of this function, that the type variable excepts is constrained to the kind [Type]

@JonathanLorimer JonathanLorimer marked this pull request as ready for review April 3, 2021 19:21
@JonathanLorimer
Copy link
Author

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant