{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
module Serokell.Util.Exceptions
( TextException (..)
, throwText
, EmptyException (..)
, throwEmpty
, eitherToFail
) where
import Universum
import Formatting (bprint, stext, string, (%))
import qualified Control.Monad as Monad
import qualified Formatting.Buildable
instance Formatting.Buildable.Buildable SomeException where
build e =
case fromException e of
Nothing -> bprint string (displayException e)
Just (TextException msg) -> bprint ("TextException: " %stext) msg
newtype TextException = TextException
{ teMessage :: Text
} deriving (Show,Typeable)
instance Exception TextException
instance Formatting.Buildable.Buildable TextException where
build = bprint ("TextException: " %stext) . teMessage
throwText :: MonadThrow m
=> Text -> m a
throwText = throwM . TextException
data EmptyException = EmptyException
deriving (Show, Typeable)
instance Exception EmptyException
throwEmpty :: MonadThrow m => m a
throwEmpty = throwM EmptyException
eitherToFail :: (Monad m, e ~ SomeException) => Either e a -> m a
eitherToFail = either (Monad.fail . show) return