{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Antiope.Core.Error
( handle404ToNone
, handleServiceError
) where
import Control.Monad.Catch (catch, throwM)
import Network.AWS (Error (..), MonadAWS, ServiceError (..))
import Network.HTTP.Types.Status (Status (..))
handle404ToNone :: MonadAWS m
=> m a
-> m (Maybe a)
handle404ToNone f =
handleServiceError f Just $ \case
(Status 404 _) -> Just Nothing
_ -> Nothing
handleServiceError :: MonadAWS m
=> m a
-> (a -> b)
-> (Status -> Maybe b)
-> m b
handleServiceError ma success failure =
(success <$> ma) `catch` \(err :: Error) -> case err of
(ServiceError (ServiceError' _ status _ _ _ _)) -> maybe (throwM err) pure (failure status)
_ -> throwM err