{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE NoImplicitPrelude
, ExistentialQuantification
, MagicHash
, PatternSynonyms
#-}
{-# LANGUAGE ImplicitParams #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE ViewPatterns #-}
{-# OPTIONS_HADDOCK not-home #-}
module GHC.Internal.Exception
(
Exception(..)
, SomeException(..)
, displayExceptionWithInfo
, someExceptionContext
, addExceptionContext
, throw
, ArithException(..)
, divZeroException
, overflowException
, ratioZeroDenomException
, underflowException
, ErrorCall(.., ErrorCallWithLocation)
, errorCallException
, errorCallWithCallStackException
, toExceptionWithBacktrace
, CallStack, fromCallSiteList, getCallStack, prettyCallStack
, prettyCallStackLines
, SrcLoc(..), prettySrcLoc
) where
import GHC.Internal.Base
import GHC.Internal.Show
import GHC.Internal.Stack.Types
import GHC.Internal.IO.Unsafe
import {-# SOURCE #-} GHC.Internal.Stack (prettyCallStackLines, prettyCallStack, prettySrcLoc, withFrozenCallStack)
import {-# SOURCE #-} GHC.Internal.Exception.Backtrace (collectExceptionAnnotation)
import GHC.Internal.Exception.Context (SomeExceptionAnnotation(..))
import GHC.Internal.Exception.Type
throw :: forall (r :: RuntimeRep). forall (a :: TYPE r). forall e.
(HasCallStack, Exception e) => e -> a
throw :: forall a e. (HasCallStack, Exception e) => e -> a
throw e
e =
let se :: SomeException
!se :: SomeException
se = SomeException -> SomeException
forall a. a -> a
noinline (IO SomeException -> SomeException
forall a. IO a -> a
unsafePerformIO (e -> IO SomeException
forall e. (HasCallStack, Exception e) => e -> IO SomeException
toExceptionWithBacktrace e
e))
in SomeException -> a
forall a b. a -> b
raise# SomeException
se
toExceptionWithBacktrace :: (HasCallStack, Exception e)
=> e -> IO SomeException
toExceptionWithBacktrace :: forall e. (HasCallStack, Exception e) => e -> IO SomeException
toExceptionWithBacktrace e
e
| e -> Bool
forall e. Exception e => e -> Bool
backtraceDesired e
e = do
SomeExceptionAnnotation ea <- IO SomeExceptionAnnotation
HasCallStack => IO SomeExceptionAnnotation
collectExceptionAnnotation
return (addExceptionContext ea (toException e))
| Bool
otherwise = SomeException -> IO SomeException
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (e -> SomeException
forall e. Exception e => e -> SomeException
toException e
e)
data ErrorCall = ErrorCall String
deriving ( ErrorCall -> ErrorCall -> Bool
(ErrorCall -> ErrorCall -> Bool)
-> (ErrorCall -> ErrorCall -> Bool) -> Eq ErrorCall
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ErrorCall -> ErrorCall -> Bool
== :: ErrorCall -> ErrorCall -> Bool
$c/= :: ErrorCall -> ErrorCall -> Bool
/= :: ErrorCall -> ErrorCall -> Bool
Eq
, Eq ErrorCall
Eq ErrorCall =>
(ErrorCall -> ErrorCall -> Ordering)
-> (ErrorCall -> ErrorCall -> Bool)
-> (ErrorCall -> ErrorCall -> Bool)
-> (ErrorCall -> ErrorCall -> Bool)
-> (ErrorCall -> ErrorCall -> Bool)
-> (ErrorCall -> ErrorCall -> ErrorCall)
-> (ErrorCall -> ErrorCall -> ErrorCall)
-> Ord ErrorCall
ErrorCall -> ErrorCall -> Bool
ErrorCall -> ErrorCall -> Ordering
ErrorCall -> ErrorCall -> ErrorCall
forall a.
Eq a =>
(a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
$ccompare :: ErrorCall -> ErrorCall -> Ordering
compare :: ErrorCall -> ErrorCall -> Ordering
$c< :: ErrorCall -> ErrorCall -> Bool
< :: ErrorCall -> ErrorCall -> Bool
$c<= :: ErrorCall -> ErrorCall -> Bool
<= :: ErrorCall -> ErrorCall -> Bool
$c> :: ErrorCall -> ErrorCall -> Bool
> :: ErrorCall -> ErrorCall -> Bool
$c>= :: ErrorCall -> ErrorCall -> Bool
>= :: ErrorCall -> ErrorCall -> Bool
$cmax :: ErrorCall -> ErrorCall -> ErrorCall
max :: ErrorCall -> ErrorCall -> ErrorCall
$cmin :: ErrorCall -> ErrorCall -> ErrorCall
min :: ErrorCall -> ErrorCall -> ErrorCall
Ord
)
{-# DEPRECATED ErrorCallWithLocation "ErrorCallWithLocation has been deprecated in favour of ErrorCall (which does not have a location). Backtraces are now handled by the backtrace exception mechanisms exclusively." #-}
pattern ErrorCallWithLocation :: String -> String -> ErrorCall
pattern $mErrorCallWithLocation :: forall {r}.
ErrorCall -> ([Char] -> [Char] -> r) -> ((# #) -> r) -> r
$bErrorCallWithLocation :: [Char] -> [Char] -> ErrorCall
ErrorCallWithLocation err loc <- ErrorCall ((\[Char]
err -> ([Char]
err, [Char] -> [Char]
forall a. HasCallStack => [Char] -> a
error [Char]
"ErrorCallWithLocation has been deprecated in favour of ErrorCall (which does not have a location). Backtraces are now handled by the backtrace exception mechanisms exclusively.")) -> (err, loc))
where ErrorCallWithLocation [Char]
err [Char]
_ = [Char] -> ErrorCall
ErrorCall [Char]
err
{-# COMPLETE ErrorCallWithLocation #-}
instance Exception ErrorCall
instance Show ErrorCall where
showsPrec :: Int -> ErrorCall -> [Char] -> [Char]
showsPrec Int
_ (ErrorCall [Char]
err) = [Char] -> [Char] -> [Char]
showString [Char]
err
errorCallException :: String -> SomeException
errorCallException :: [Char] -> SomeException
errorCallException [Char]
s = ErrorCall -> SomeException
forall e. Exception e => e -> SomeException
toException ([Char] -> ErrorCall
ErrorCall [Char]
s)
errorCallWithCallStackException :: String -> CallStack -> SomeException
errorCallWithCallStackException :: [Char] -> CallStack -> SomeException
errorCallWithCallStackException [Char]
s CallStack
stk = IO SomeException -> SomeException
forall a. IO a -> a
unsafeDupablePerformIO (IO SomeException -> SomeException)
-> IO SomeException -> SomeException
forall a b. (a -> b) -> a -> b
$ do
(HasCallStack => IO SomeException) -> IO SomeException
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack ((HasCallStack => IO SomeException) -> IO SomeException)
-> (HasCallStack => IO SomeException) -> IO SomeException
forall a b. (a -> b) -> a -> b
$ ErrorCall -> IO SomeException
forall e. (HasCallStack, Exception e) => e -> IO SomeException
toExceptionWithBacktrace ([Char] -> ErrorCall
ErrorCall [Char]
s)
where ?callStack = CallStack
stk