Skip to content

Instantly share code, notes, and snippets.

@harpocrates
Last active March 23, 2017 04:43
Show Gist options
  • Save harpocrates/923b6ccceeec3619c68928336e42c6de to your computer and use it in GitHub Desktop.
Save harpocrates/923b6ccceeec3619c68928336e42c6de to your computer and use it in GitHub Desktop.
Do the unthinkable: catch errors in pure code
-- 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