{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module OpenTelemetry.Trace.ExceptionHandler (
ExceptionClassification (..),
ExceptionResponse (..),
ExceptionHandler,
defaultExceptionResponse,
ignoreExceptionType,
ignoreExceptionMatching,
recordExceptionType,
recordExceptionMatching,
classifyException,
exitSuccessHandler,
resolveException,
) where
import Control.Exception (Exception (..), SomeException (..))
import qualified Data.HashMap.Strict as H
import OpenTelemetry.Internal.Trace.Types
import System.Exit (ExitCode (..))
ignoreExceptionType :: forall e. (Exception e) => ExceptionHandler
ignoreExceptionType :: forall e. Exception e => ExceptionHandler
ignoreExceptionType (SomeException e
ex) = case forall e. Exception e => SomeException -> Maybe e
fromException @e (e -> SomeException
forall e. (Exception e, HasExceptionContext) => e -> SomeException
SomeException e
ex) of
Just e
_ -> ExceptionResponse -> Maybe ExceptionResponse
forall a. a -> Maybe a
Just (ExceptionResponse -> Maybe ExceptionResponse)
-> ExceptionResponse -> Maybe ExceptionResponse
forall a b. (a -> b) -> a -> b
$ ExceptionClassification -> AttributeMap -> ExceptionResponse
ExceptionResponse ExceptionClassification
IgnoredException AttributeMap
forall k v. HashMap k v
H.empty
Maybe e
Nothing -> Maybe ExceptionResponse
forall a. Maybe a
Nothing
ignoreExceptionMatching :: forall e. (Exception e) => (e -> Bool) -> ExceptionHandler
ignoreExceptionMatching :: forall e. Exception e => (e -> Bool) -> ExceptionHandler
ignoreExceptionMatching e -> Bool
p (SomeException e
ex) = case forall e. Exception e => SomeException -> Maybe e
fromException @e (e -> SomeException
forall e. (Exception e, HasExceptionContext) => e -> SomeException
SomeException e
ex) of
Just e
e
| e -> Bool
p e
e -> ExceptionResponse -> Maybe ExceptionResponse
forall a. a -> Maybe a
Just (ExceptionResponse -> Maybe ExceptionResponse)
-> ExceptionResponse -> Maybe ExceptionResponse
forall a b. (a -> b) -> a -> b
$ ExceptionClassification -> AttributeMap -> ExceptionResponse
ExceptionResponse ExceptionClassification
IgnoredException AttributeMap
forall k v. HashMap k v
H.empty
Maybe e
_ -> Maybe ExceptionResponse
forall a. Maybe a
Nothing
recordExceptionType :: forall e. (Exception e) => ExceptionHandler
recordExceptionType :: forall e. Exception e => ExceptionHandler
recordExceptionType (SomeException e
ex) = case forall e. Exception e => SomeException -> Maybe e
fromException @e (e -> SomeException
forall e. (Exception e, HasExceptionContext) => e -> SomeException
SomeException e
ex) of
Just e
_ -> ExceptionResponse -> Maybe ExceptionResponse
forall a. a -> Maybe a
Just (ExceptionResponse -> Maybe ExceptionResponse)
-> ExceptionResponse -> Maybe ExceptionResponse
forall a b. (a -> b) -> a -> b
$ ExceptionClassification -> AttributeMap -> ExceptionResponse
ExceptionResponse ExceptionClassification
RecordedException AttributeMap
forall k v. HashMap k v
H.empty
Maybe e
Nothing -> Maybe ExceptionResponse
forall a. Maybe a
Nothing
recordExceptionMatching :: forall e. (Exception e) => (e -> Bool) -> ExceptionHandler
recordExceptionMatching :: forall e. Exception e => (e -> Bool) -> ExceptionHandler
recordExceptionMatching e -> Bool
p (SomeException e
ex) = case forall e. Exception e => SomeException -> Maybe e
fromException @e (e -> SomeException
forall e. (Exception e, HasExceptionContext) => e -> SomeException
SomeException e
ex) of
Just e
e
| e -> Bool
p e
e -> ExceptionResponse -> Maybe ExceptionResponse
forall a. a -> Maybe a
Just (ExceptionResponse -> Maybe ExceptionResponse)
-> ExceptionResponse -> Maybe ExceptionResponse
forall a b. (a -> b) -> a -> b
$ ExceptionClassification -> AttributeMap -> ExceptionResponse
ExceptionResponse ExceptionClassification
RecordedException AttributeMap
forall k v. HashMap k v
H.empty
Maybe e
_ -> Maybe ExceptionResponse
forall a. Maybe a
Nothing
classifyException :: forall e. (Exception e) => (e -> ExceptionResponse) -> ExceptionHandler
classifyException :: forall e.
Exception e =>
(e -> ExceptionResponse) -> ExceptionHandler
classifyException e -> ExceptionResponse
f (SomeException e
ex) = case forall e. Exception e => SomeException -> Maybe e
fromException @e (e -> SomeException
forall e. (Exception e, HasExceptionContext) => e -> SomeException
SomeException e
ex) of
Just e
e -> ExceptionResponse -> Maybe ExceptionResponse
forall a. a -> Maybe a
Just (e -> ExceptionResponse
f e
e)
Maybe e
Nothing -> Maybe ExceptionResponse
forall a. Maybe a
Nothing
exitSuccessHandler :: ExceptionHandler
exitSuccessHandler :: ExceptionHandler
exitSuccessHandler = forall e. Exception e => (e -> Bool) -> ExceptionHandler
ignoreExceptionMatching @ExitCode (ExitCode -> ExitCode -> Bool
forall a. Eq a => a -> a -> Bool
== ExitCode
ExitSuccess)