Exception annotations can be lost and duplicated
# Problem 1: exception annotations may be lost (ghc 9.10, 9.12 and 9.14)
I would expect
```hs
catchAndThrow :: IO a -> IO a
catchAndThrow = handle $ \(e :: SomeException) -> throwIO e
```
to be essentially an identity (modulo async exceptions, perhaps). However, it is not. In the `Exception` instance for `SomeException`, the definition of `toException` clears the context. Looking at the code, this seems intentional, but it feels highly counter-intuitive to me. Consider:
```hs
{-# LANGUAGE DeriveAnyClass #-}
module ReproLostAnnotations (main) where
import Control.Exception
import Control.Exception.Annotation
data MyException = MyException
deriving (Show, Exception)
data MyAnnotation = MyAnnotation
deriving (Show, ExceptionAnnotation)
catchAndThrow :: IO a -> IO a
catchAndThrow = handle $ \(e :: SomeException) -> throwIO e
main :: IO ()
main =
catchAndThrow $
annotateIO MyAnnotation $
throwIO MyException
```
Without the `catchAndThrow`, we see the `MyAnnotation` exception, but when we add it, it's suddenly gone.
This pattern (catch and then rethrow, probably depending on various conditions) I think is quite a common pattern.
# Problem 2: exception annotations may be duplicated (ghc 9.10 only)
Consider this minor variation of `catchAndThrow`:
```hs
catchAndThrowWithContext :: IO a -> IO a
catchAndThrowWithContext =
handle $ \e@(ExceptionWithContext _ctxt (_ :: SomeException)) ->
throwIO e
```
This pattern might quite naturally arise if someone wants to catch all exceptions, but also wants convenient access to the exception context.
As for `catchAndThrow`, I would expect this to be essentially an identity, but now we have the _opposite_ problem. The `fromException` instance of `ExceptionWithContext` results in the context being duplicated in this case. Consider this small example, where `showException` is a function that shows the actual structure of `SomeException`:
```hs
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE ImplicitParams #-}
{-# LANGUAGE MultiWayIf #-}
module ReproDuplicatedAnnotations (main) where
import Control.Exception
import Control.Exception.Annotation
import Control.Exception.Context
import Data.Typeable
data MyException = MyException
deriving (Show, Exception)
data MyAnnotation = MyAnnotation
deriving (Show, ExceptionAnnotation)
showException :: SomeException -> String
showException (SomeException e) = concat [
"SomeException {"
, displayExceptionContext ctxt
, "} ("
, if | Just (e' :: SomeException) <- cast e ->
showException e'
| Just (e' :: MyException) <- cast e ->
show e'
| otherwise ->
"<unknown exception of type " ++ show (typeOf e) ++ ">"
, ")"
]
where
ctxt :: ExceptionContext
ctxt = ?exceptionContext
catchThenShow :: IO () -> IO ()
catchThenShow = handle $ \(e :: SomeException) ->
putStrLn $ showException e
catchAndThrowWithContext :: IO a -> IO a
catchAndThrowWithContext =
handle $ \e@(ExceptionWithContext _ctxt (_ :: SomeException)) ->
throwIO e
main :: IO ()
main =
catchThenShow $
catchAndThrowWithContext $
annotateIO MyAnnotation $
throwIO MyException
```
If we omit that call to `catchAndThrowWithContext`, this prints
```
SomeException {MyAnnotation
HasCallStack backtrace: ...
} (MyException)
```
But when we include the call to `catchAndThrowWithContext`, we instead get
```
SomeException {HasCallStack backtrace: ..
MyAnnotation
HasCallStack backtrace: ..
} (SomeException {MyAnnotation
HasCallStack backtrace: ..
} (MyException))
```
# Interaction between these two bugs
Since the first of these bugs _loses_ exception annotations, and the other _duplicates_ them, we get the utterly bizarre behaviour that surrounding a piece of code with a call to
`catchAndThrowWithContext` may result in some exception annotations making it all the way to the top-level exception handler, but without it they don't, because some `catchAndThrow` in the middle strips the annotations (but only one level!).
For completeness sake a reproducer for this also:
```hs
{-# LANGUAGE DeriveAnyClass #-}
module ReproCombined (main) where
import Control.Exception
import Control.Exception.Annotation
data MyException = MyException
deriving (Show, Exception)
data MyAnnotation = MyAnnotation
deriving (Show, ExceptionAnnotation)
catchAndThrow :: IO a -> IO a
catchAndThrow = handle $ \(e :: SomeException) -> throwIO e
catchAndThrowWithContext :: IO a -> IO a
catchAndThrowWithContext =
handle $ \e@(ExceptionWithContext _ctxt (_ :: SomeException)) ->
throwIO e
main :: IO ()
main =
catchAndThrow $
catchAndThrowWithContext $
annotateIO MyAnnotation $
throwIO MyException
```
_With_ the call to `catchAndThrowWithContext`, we see `MyAnnotation` in the top-level exception handler; omit that call, and we don't. That was quite the head-scratcher!
With thanks to @alt-romes for helping me debug this.
issue