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.Except (MonadError(..))
19 import Control.Lens ((#))
21 import Servant.Auth.Server (AuthResult(..))
23 import Gargantext.Prelude
24 import Gargantext.API.Prelude (GargServerM, _ServerError)
25 import Gargantext.API.Routes (GargPrivateAPI, serverPrivateGargAPI')
27 class ThrowAll' e a | a -> e where
28 -- | 'throwAll' is a convenience function to throw errors across an entire
32 -- > throwAll err400 :: Handler a :<|> Handler b :<|> Handler c
33 -- > == throwError err400 :<|> throwError err400 :<|> err400
36 instance (ThrowAll' e a, ThrowAll' e b) => ThrowAll' e (a :<|> b) where
37 throwAll' e = throwAll' e :<|> throwAll' e
39 -- Really this shouldn't be necessary - ((->) a) should be an instance of
41 instance {-# OVERLAPPING #-} ThrowAll' e b => ThrowAll' e (a -> b) where
42 throwAll' e = const $ throwAll' e
44 instance {-# OVERLAPPABLE #-} (MonadError e m) => ThrowAll' e (m a) where
45 throwAll' = throwError
47 serverPrivateGargAPI :: GargServerM env err GargPrivateAPI
48 serverPrivateGargAPI (Authenticated auser) = serverPrivateGargAPI' auser
49 serverPrivateGargAPI _ = throwAll' (_ServerError # err401)
50 -- Here throwAll' requires a concrete type for the monad.