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 MonoLocalBinds #-}
14 {-# LANGUAGE TypeOperators #-}
15 {-# LANGUAGE UndecidableInstances #-}
17 module Gargantext.API.ThrowAll where
19 import Control.Monad.Except (MonadError(..))
20 import Control.Lens ((#))
22 import Servant.Auth.Server (AuthResult(..))
24 import Gargantext.Prelude
25 import Gargantext.API.Prelude (GargServerM, _ServerError)
26 import Gargantext.API.Routes (GargPrivateAPI, serverPrivateGargAPI')
28 class ThrowAll' e a | a -> e where
29 -- | 'throwAll' is a convenience function to throw errors across an entire
33 -- > throwAll err400 :: Handler a :<|> Handler b :<|> Handler c
34 -- > == throwError err400 :<|> throwError err400 :<|> err400
37 instance (ThrowAll' e a, ThrowAll' e b) => ThrowAll' e (a :<|> b) where
38 throwAll' e = throwAll' e :<|> throwAll' e
40 -- Really this shouldn't be necessary - ((->) a) should be an instance of
42 instance {-# OVERLAPPING #-} ThrowAll' e b => ThrowAll' e (a -> b) where
43 throwAll' e = const $ throwAll' e
45 instance {-# OVERLAPPABLE #-} (MonadError e m) => ThrowAll' e (m a) where
46 throwAll' = throwError
48 serverPrivateGargAPI :: MimeRender JSON err => GargServerM env err GargPrivateAPI
49 serverPrivateGargAPI (Authenticated auser) = serverPrivateGargAPI' auser
50 serverPrivateGargAPI _ = throwAll' (_ServerError # err401)
51 -- Here throwAll' requires a concrete type for the monad.