Skip to content

[#7] Custom Errors for runEIO#13

Open
JonathanLorimer wants to merge 7 commits into
kowainik:mainfrom
JonathanLorimer:runEIO-custom-type-errors
Open

[#7] Custom Errors for runEIO#13
JonathanLorimer wants to merge 7 commits into
kowainik:mainfrom
JonathanLorimer:runEIO-custom-type-errors

Conversation

@JonathanLorimer
Copy link
Copy Markdown

@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 thread .gitignore
Comment on lines +25 to +33
ghcid.txt

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

# Nix
shell.nix

Copy link
Copy Markdown
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 thread src/EIO/TypeErrors.hs
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
Copy Markdown
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.

Comment thread src/EIO.hs Outdated
@since 0.0.0.0
-}
runEIO :: EIO '[] () -> IO ()
runEIO :: DisallowUnhandledExceptions excepts => EIO excepts () -> IO ()
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

1 participant