-- | Like the spoon package, but for catching one specific exception
-- type and returning it.

module Control.Spork
    ( spork
    ) where

import Control.Exception
import System.IO.Unsafe

-- | Evaluate `a` and return left if it throws a pure exception.
spork
  :: Exception e
  => a -> Either e a
spork :: forall e a. Exception e => a -> Either e a
spork a
a =
  forall a. IO a -> a
unsafePerformIO forall a b. (a -> b) -> a -> b
$
  (forall a b. b -> Either a b
Right forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
`fmap` forall a. a -> IO a
evaluate a
a) forall a. IO a -> [Handler a] -> IO a
`catches` [forall a e. Exception e => (e -> IO a) -> Handler a
Handler (\e
e -> forall (f :: * -> *) a. Applicative f => a -> f a
pure (forall a b. a -> Either a b
Left e
e))]
{-# INLINEABLE spork #-}