{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
module Capnp.Rpc.Errors
(
wrapException
, eMethodUnimplemented
, eUnimplemented
, eDisconnected
, eFailed
, throwFailed
) where
import Data.Default (Default(def))
import Data.Maybe (fromMaybe)
import Data.String (fromString)
import Data.Text (Text)
import qualified Control.Exception.Safe as E
import Capnp.Gen.Capnp.Rpc.New
eFailed :: Text -> Parsed Exception
eFailed :: Text -> Parsed Exception
eFailed Text
reason = R:ParsedException
forall a. Default a => a
def
{ $sel:type_:Exception :: Parsed Exception'Type
type_ = Parsed Exception'Type
Exception'Type
Exception'Type'failed
, $sel:reason:Exception :: Parsed Text
reason = Text
Parsed Text
reason
}
eDisconnected :: Parsed Exception
eDisconnected :: Parsed Exception
eDisconnected = R:ParsedException
forall a. Default a => a
def
{ $sel:type_:Exception :: Parsed Exception'Type
type_ = Parsed Exception'Type
Exception'Type
Exception'Type'disconnected
, $sel:reason:Exception :: Parsed Text
reason = Parsed Text
"Disconnected"
}
eMethodUnimplemented :: Parsed Exception
eMethodUnimplemented :: Parsed Exception
eMethodUnimplemented =
Text -> Parsed Exception
eUnimplemented Text
"Method unimplemented"
eUnimplemented :: Text -> Parsed Exception
eUnimplemented :: Text -> Parsed Exception
eUnimplemented Text
reason = R:ParsedException
forall a. Default a => a
def
{ $sel:type_:Exception :: Parsed Exception'Type
type_ = Parsed Exception'Type
Exception'Type
Exception'Type'unimplemented
, $sel:reason:Exception :: Parsed Text
reason = Text
Parsed Text
reason
}
instance E.Exception (Parsed Exception)
wrapException :: Bool -> E.SomeException -> Parsed Exception
wrapException :: Bool -> SomeException -> Parsed Exception
wrapException Bool
debugMode SomeException
e = Parsed Exception -> Maybe (Parsed Exception) -> Parsed Exception
forall a. a -> Maybe a -> a
fromMaybe
R:ParsedException
forall a. Default a => a
def { $sel:type_:Exception :: Parsed Exception'Type
type_ = Parsed Exception'Type
Exception'Type
Exception'Type'failed
, $sel:reason:Exception :: Parsed Text
reason =
if Bool
debugMode then
Text
"Unhandled exception: " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> String -> Text
forall a. IsString a => String -> a
fromString (SomeException -> String
forall a. Show a => a -> String
show SomeException
e)
else
Parsed Text
"Unhandled exception"
}
(SomeException -> Maybe (Parsed Exception)
forall e. Exception e => SomeException -> Maybe e
E.fromException SomeException
e)
throwFailed :: E.MonadThrow m => Text -> m a
throwFailed :: Text -> m a
throwFailed = Parsed Exception -> m a
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
E.throwM (Parsed Exception -> m a)
-> (Text -> Parsed Exception) -> Text -> m a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> Parsed Exception
eFailed