packages feed

otel-effectful-1.0.0: src/Effectful/OpenTelemetry/Protocol/Exception.hs

module Effectful.OpenTelemetry.Protocol.Exception where

import Data.Typeable (cast)
import Effectful.Exception
import Effectful.GrpcClient (GrpcError)
import Effectful.Http2Client (ClientError)
import Effectful.HttpClient (HttpException)
import Prelude

-- | Any 'Exception' thrown in @otel-effectful@ is encapsulated in a 'SomeOTLPException'.
data SomeOTLPException = forall e. (Exception e) => SomeOTLPException e

fromOTLPException :: (Exception e) => SomeOTLPException -> Maybe e
fromOTLPException (SomeOTLPException e) = fromException . toException $ e

instance Show SomeOTLPException where
    show (SomeOTLPException e) = show e

instance Exception SomeOTLPException where
    displayException (SomeOTLPException e) = displayException e
    backtraceDesired (SomeOTLPException e) = backtraceDesired e

otlpExceptionToException :: (Exception e) => e -> SomeException
otlpExceptionToException = toException . SomeOTLPException

otlpExceptionFromException :: (Exception e) => SomeException -> Maybe e
otlpExceptionFromException ex = do
    SomeOTLPException e <- fromException ex
    cast e

data ConnectionTimeout = ConnectionTimeout
    deriving stock (Show)

instance Exception ConnectionTimeout where
    toException = otlpExceptionToException
    fromException = otlpExceptionFromException
    displayException ConnectionTimeout = "connection timed out"

newtype OTLPHttpException = OTLPHttpException HttpException
    deriving newtype (Show)

instance Exception OTLPHttpException where
    toException = otlpExceptionToException
    fromException = otlpExceptionFromException
    displayException (OTLPHttpException e) = displayException e

newtype OTLPClientError = OTLPClientError ClientError
    deriving newtype (Show)

instance Exception OTLPClientError where
    toException = otlpExceptionToException
    fromException = otlpExceptionFromException
    displayException (OTLPClientError e) = displayException e

newtype OTLPGrpcError = OTLPGrpcError GrpcError
    deriving newtype (Show)

instance Exception OTLPGrpcError where
    toException = otlpExceptionToException
    fromException = otlpExceptionFromException
    displayException (OTLPGrpcError e) = displayException e