{-# LANGUAGE CPP #-}
-- |
-- Module      : Data.Text.Internal.Exception
-- Copyright   : (c) 2026 text maintainers
-- License     : BSD-style
-- Maintainer  : lysxia@gmail.com
-- Portability : GHC
--
-- /Warning/: this is an internal module, and does not have a stable
-- API or name. Functions in this module may not check or enforce
-- preconditions expected by public modules. Use at your own risk!
--
-- Compatibility shims for re-throwing exceptions.
-- GHC 9.12 implements this API, and we fake it for older GHCs: rethrow is just throw.
module Data.Text.Internal.Exception
  ( ExceptionWithContext(..)
  , ExceptionContext
  , catchNoPropagate
  , rethrowIO
  , someExceptionContext
  , rethrow
  ) where

#if __GLASGOW_HASKELL__ >= 912
import Control.Exception
import Control.Exception.Context (ExceptionContext)

rethrow :: Exception e => ExceptionWithContext e -> a
rethrow = throw . NoBacktrace
#else
import Control.Exception (Exception, SomeException)
import qualified Control.Exception as E
import GHC.Stack (HasCallStack, withFrozenCallStack)

type ExceptionContext = ()
data ExceptionWithContext e = ExceptionWithContext ExceptionContext e

catchNoPropagate :: Exception e => IO a -> (ExceptionWithContext e -> IO a) -> IO a
catchNoPropagate :: forall e a.
Exception e =>
IO a -> (ExceptionWithContext e -> IO a) -> IO a
catchNoPropagate IO a
io ExceptionWithContext e -> IO a
h = IO a -> (e -> IO a) -> IO a
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
E.catch IO a
io (ExceptionWithContext e -> IO a
h (ExceptionWithContext e -> IO a)
-> (e -> ExceptionWithContext e) -> e -> IO a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ExceptionContext -> e -> ExceptionWithContext e
forall e. ExceptionContext -> e -> ExceptionWithContext e
ExceptionWithContext ())

rethrowIO :: Exception e => ExceptionWithContext e -> IO a
rethrowIO :: forall e a. Exception e => ExceptionWithContext e -> IO a
rethrowIO (ExceptionWithContext () e
e) = (HasCallStack => IO a) -> IO a
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack (e -> IO a
forall e a. Exception e => e -> IO a
E.throwIO e
e)

someExceptionContext :: SomeException -> ExceptionContext
someExceptionContext :: SomeException -> ExceptionContext
someExceptionContext SomeException
_ = ()

rethrow :: (Exception e, HasCallStack) => ExceptionWithContext e -> a
rethrow :: forall e a.
(Exception e, HasCallStack) =>
ExceptionWithContext e -> a
rethrow (ExceptionWithContext () e
e) = (HasCallStack => a) -> a
forall a. HasCallStack => (HasCallStack => a) -> a
withFrozenCallStack (e -> a
forall a e. Exception e => e -> a
E.throw e
e)
#endif