2021-04-10 20:34:26 +03:00
|
|
|
{-# LANGUAGE ScopedTypeVariables #-}
|
|
|
|
|
2021-05-11 02:36:22 +03:00
|
|
|
module Witch.TryFromException where
|
2021-04-10 20:34:26 +03:00
|
|
|
|
|
|
|
import qualified Control.Exception as Exception
|
|
|
|
import qualified Data.Proxy as Proxy
|
|
|
|
import qualified Data.Typeable as Typeable
|
|
|
|
|
2021-05-11 02:31:18 +03:00
|
|
|
-- | This exception is thrown when a @TryFrom@ conversion fails. It has the
|
2021-04-18 21:01:05 +03:00
|
|
|
-- original @source@ value that caused the failure and it knows the @target@
|
2021-04-23 16:37:26 +03:00
|
|
|
-- type it was trying to convert into. It also has an optional
|
|
|
|
-- 'Exception.SomeException' for communicating what went wrong while
|
|
|
|
-- converting.
|
2021-05-11 02:36:22 +03:00
|
|
|
data TryFromException source target = TryFromException
|
2021-04-23 16:01:03 +03:00
|
|
|
source
|
|
|
|
(Maybe Exception.SomeException)
|
2021-04-10 20:34:26 +03:00
|
|
|
|
|
|
|
instance
|
|
|
|
( Show source
|
|
|
|
, Typeable.Typeable source
|
|
|
|
, Typeable.Typeable target
|
2021-05-11 02:36:22 +03:00
|
|
|
) => Show (TryFromException source target) where
|
|
|
|
showsPrec d (TryFromException x e) =
|
2021-04-18 18:28:02 +03:00
|
|
|
showParen (d > 10)
|
2021-05-11 02:36:22 +03:00
|
|
|
$ showString "TryFromException @"
|
2021-04-26 14:55:52 +03:00
|
|
|
. showsPrec 11 (Typeable.typeRep (Proxy.Proxy :: Proxy.Proxy source))
|
|
|
|
. showString " @"
|
|
|
|
. showsPrec 11 (Typeable.typeRep (Proxy.Proxy :: Proxy.Proxy target))
|
|
|
|
. showChar ' '
|
2021-04-18 18:28:02 +03:00
|
|
|
. showsPrec 11 x
|
2021-04-23 05:47:04 +03:00
|
|
|
. showChar ' '
|
|
|
|
. showsPrec 11 e
|
2021-04-17 17:17:32 +03:00
|
|
|
|
|
|
|
instance
|
|
|
|
( Show source
|
|
|
|
, Typeable.Typeable source
|
|
|
|
, Typeable.Typeable target
|
2021-05-11 02:36:22 +03:00
|
|
|
) => Exception.Exception (TryFromException source target)
|