2 Module : Gargantext.API.ThrowAll
3 Description : ThrowAll class and instance
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
12 {-# LANGUAGE FunctionalDependencies #-}
13 {-# LANGUAGE TypeOperators #-}
14 {-# LANGUAGE UndecidableInstances #-}
16 module Gargantext.API.ThrowAll where
18 import Control.Monad.Error.Class (MonadError(..))
19 import Control.Lens ((#))
21 import Servant.Auth.Server (AuthResult(..))
22 import Gargantext.Prelude
23 import Gargantext.API.Prelude (GargServerM, _ServerError)
24 import Gargantext.API.Routes (GargPrivateAPI, serverPrivateGargAPI')
26 class ThrowAll' e a | a -> e where
27 -- | 'throwAll' is a convenience function to throw errors across an entire
31 -- > throwAll err400 :: Handler a :<|> Handler b :<|> Handler c
32 -- > == throwError err400 :<|> throwError err400 :<|> err400
35 instance (ThrowAll' e a, ThrowAll' e b) => ThrowAll' e (a :<|> b) where
36 throwAll' e = throwAll' e :<|> throwAll' e
38 -- Really this shouldn't be necessary - ((->) a) should be an instance of
40 instance {-# OVERLAPPING #-} ThrowAll' e b => ThrowAll' e (a -> b) where
41 throwAll' e = const $ throwAll' e
43 instance {-# OVERLAPPABLE #-} (MonadError e m) => ThrowAll' e (m a) where
44 throwAll' = throwError
46 serverPrivateGargAPI :: GargServerM env err GargPrivateAPI
47 serverPrivateGargAPI (Authenticated auser) = serverPrivateGargAPI' auser
48 serverPrivateGargAPI _ = throwAll' (_ServerError # err401)
49 -- Here throwAll' requires a concrete type for the monad.