Last active
March 23, 2017 04:43
-
-
Save harpocrates/923b6ccceeec3619c68928336e42c6de to your computer and use it in GitHub Desktop.
Do the unthinkable: catch errors in pure code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Not for the weak at heart. | |
-- If you use this, you should feel bad about yourself! | |
import Control.Exception | |
import System.IO.Unsafe | |
-- Given a value, check if it has anything undefined in WNHF. If so, return the exception message, else the value | |
fromError :: a -> Either String a | |
fromError x = unsafePerformIO $ catch (Right x <$ evaluate x) getMessage | |
where | |
getMessage :: SomeException -> IO (Either String a) | |
getMessage = pure . Left . show |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment