{-# LANGUAGE CPP                #-}
{-# LANGUAGE OverloadedStrings  #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TemplateHaskell    #-}
#if __GLASGOW_HASKELL__ >= 806
{-# LANGUAGE DerivingVia        #-}
#endif
{-# OPTIONS_GHC -fno-warn-orphans #-}
{-|
Module:      TextShow.Control.Exception
Copyright:   (C) 2014-2017 Ryan Scott
License:     BSD-style (see the file LICENSE)
Maintainer:  Ryan Scott
Stability:   Provisional
Portability: GHC

'TextShow' instances for 'Exception' data types.

/Since: 2/
-}
module TextShow.Control.Exception () where

import Control.Exception.Base

import Data.Text.Lazy.Builder (fromString)
#if MIN_VERSION_base(4,9,0)
import Data.Text.Lazy.Builder (singleton)
#endif

import Prelude ()
import Prelude.Compat

import TextShow.Classes (TextShow(..))
import TextShow.FromStringTextShow (FromStringShow(..))
import TextShow.TH.Internal (deriveTextShow)

-- | /Since: 2/
#if __GLASGOW_HASKELL__ >= 806
deriving via FromStringShow SomeException instance TextShow SomeException
#else
instance TextShow SomeException where
    showbPrec p (SomeException e) = showbPrec p $ FromStringShow e
    {-# INLINE showbPrec #-}
#endif

-- | /Since: 2/
#if __GLASGOW_HASKELL__ >= 806
deriving via FromStringShow IOException instance TextShow IOException
#else
instance TextShow IOException where
    showb = showb . FromStringShow
    {-# INLINE showb #-}
#endif

-- | /Since: 2/
instance TextShow ArithException where
    showb :: ArithException -> Builder
showb ArithException
Overflow             = Builder
"arithmetic overflow"
    showb ArithException
Underflow            = Builder
"arithmetic underflow"
    showb ArithException
LossOfPrecision      = Builder
"loss of precision"
    showb ArithException
DivideByZero         = Builder
"divide by zero"
    showb ArithException
Denormal             = Builder
"denormal"
    showb ArithException
RatioZeroDenominator = Builder
"Ratio has zero denominator"

-- | /Since: 2/
instance TextShow ArrayException where
    showb :: ArrayException -> Builder
showb (IndexOutOfBounds String
s)
        =  Builder
"array index out of range"
        Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> (if Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ String -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
s then Builder
": " Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> String -> Builder
fromString String
s
                            else Builder
forall a. Monoid a => a
mempty)
    showb (UndefinedElement String
s)
        =  Builder
"undefined array element"
        Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> (if Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ String -> Bool
forall a. [a] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null String
s then Builder
": " Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> String -> Builder
fromString String
s
                            else Builder
forall a. Monoid a => a
mempty)
    {-# INLINE showb #-}

-- | /Since: 2/
instance TextShow AssertionFailed where
    showb :: AssertionFailed -> Builder
showb (AssertionFailed String
err) = String -> Builder
fromString String
err
    {-# INLINE showb #-}

-- | /Since: 2/
instance TextShow SomeAsyncException where
    showb :: SomeAsyncException -> Builder
showb (SomeAsyncException e
e) = FromStringShow e -> Builder
forall a. TextShow a => a -> Builder
showb (FromStringShow e -> Builder) -> FromStringShow e -> Builder
forall a b. (a -> b) -> a -> b
$ e -> FromStringShow e
forall a. a -> FromStringShow a
FromStringShow e
e
    {-# INLINE showb #-}

-- | /Since: 2/
instance TextShow AsyncException where
    showb :: AsyncException -> Builder
showb AsyncException
StackOverflow = Builder
"stack overflow"
    showb AsyncException
HeapOverflow  = Builder
"heap overflow"
    showb AsyncException
ThreadKilled  = Builder
"thread killed"
    showb AsyncException
UserInterrupt = Builder
"user interrupt"
    {-# INLINE showb #-}

-- | /Since: 2/
instance TextShow NonTermination where
    showb :: NonTermination -> Builder
showb NonTermination
NonTermination = Builder
"<<loop>>"
    {-# INLINE showb #-}

-- | /Since: 2/
instance TextShow NestedAtomically where
    showb :: NestedAtomically -> Builder
showb NestedAtomically
NestedAtomically = Builder
"Control.Concurrent.STM.atomically was nested"
    {-# INLINE showb #-}

-- | /Since: 2/
instance TextShow BlockedIndefinitelyOnMVar where
    showb :: BlockedIndefinitelyOnMVar -> Builder
showb BlockedIndefinitelyOnMVar
BlockedIndefinitelyOnMVar = Builder
"thread blocked indefinitely in an MVar operation"
    {-# INLINE showb #-}

-- | /Since: 2/
instance TextShow BlockedIndefinitelyOnSTM where
    showb :: BlockedIndefinitelyOnSTM -> Builder
showb BlockedIndefinitelyOnSTM
BlockedIndefinitelyOnSTM = Builder
"thread blocked indefinitely in an STM transaction"
    {-# INLINE showb #-}

#if MIN_VERSION_base(4,8,0)
-- | Only available with @base-4.8.0.0@ or later.
--
-- /Since: 2/
instance TextShow AllocationLimitExceeded where
    showb :: AllocationLimitExceeded -> Builder
showb AllocationLimitExceeded
AllocationLimitExceeded = Builder
"allocation limit exceeded"
    {-# INLINE showb #-}
#endif

#if MIN_VERSION_base(4,9,0)
-- | Only available with @base-4.9.0.0@ or later.
--
-- /Since: 3/
instance TextShow TypeError where
    showb :: TypeError -> Builder
showb (TypeError String
err) = String -> Builder
fromString String
err
    {-# INLINE showb #-}
#endif

#if MIN_VERSION_base(4,10,0)
-- | Only available with @base-4.10.0.0@ or later.
--
-- /Since: 3.6/
instance TextShow CompactionFailed where
    showb :: CompactionFailed -> Builder
showb (CompactionFailed String
why) = String -> Builder
fromString (String
"compaction failed: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
why)
#endif

#if MIN_VERSION_base(4,11,0)
-- | Only available with @base-4.11.0.0@ or later.
--
-- /Since: 3.7.3/
instance TextShow FixIOException where
    showbPrec :: Int -> FixIOException -> Builder
showbPrec Int
_ FixIOException
FixIOException = String -> Builder
fromString String
"cyclic evaluation in fixIO"
#endif

-- | /Since: 2/
instance TextShow Deadlock where
    showb :: Deadlock -> Builder
showb Deadlock
Deadlock = Builder
"<<deadlock>>"
    {-# INLINE showb #-}

-- | /Since: 2/
instance TextShow NoMethodError where
    showb :: NoMethodError -> Builder
showb (NoMethodError String
err) = String -> Builder
fromString String
err
    {-# INLINE showb #-}

-- | /Since: 2/
instance TextShow PatternMatchFail where
    showb :: PatternMatchFail -> Builder
showb (PatternMatchFail String
err) = String -> Builder
fromString String
err
    {-# INLINE showb #-}

-- | /Since: 2/
instance TextShow RecConError where
    showb :: RecConError -> Builder
showb (RecConError String
err) = String -> Builder
fromString String
err
    {-# INLINE showb #-}

-- | /Since: 2/
instance TextShow RecSelError where
    showb :: RecSelError -> Builder
showb (RecSelError String
err) = String -> Builder
fromString String
err
    {-# INLINE showb #-}

-- | /Since: 2/
instance TextShow RecUpdError where
    showb :: RecUpdError -> Builder
showb (RecUpdError String
err) = String -> Builder
fromString String
err
    {-# INLINE showb #-}

-- | /Since: 2/
instance TextShow ErrorCall where
#if MIN_VERSION_base(4,9,0)
    showb :: ErrorCall -> Builder
showb (ErrorCallWithLocation String
err String
"")  = String -> Builder
fromString String
err
    showb (ErrorCallWithLocation String
err String
loc) =
      String -> Builder
fromString String
err Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> Char -> Builder
singleton Char
'\n' Builder -> Builder -> Builder
forall a. Semigroup a => a -> a -> a
<> String -> Builder
fromString String
loc
#else
    showb (ErrorCall err) = fromString err
#endif

-- | /Since: 2/
$(deriveTextShow ''MaskingState)