]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/API/ThrowAll.hs
Merge branch 'dev' into dev-wikidata
[gargantext.git] / src / Gargantext / API / ThrowAll.hs
1 {-|
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
8 Portability : POSIX
9
10 -}
11
12 {-# LANGUAGE FunctionalDependencies #-}
13 {-# LANGUAGE MonoLocalBinds #-}
14 {-# LANGUAGE TypeOperators #-}
15 {-# LANGUAGE UndecidableInstances #-}
16
17 module Gargantext.API.ThrowAll where
18
19 import Control.Monad.Except (MonadError(..))
20 import Control.Lens ((#))
21 import Servant
22 import Servant.Auth.Server (AuthResult(..))
23
24 import Gargantext.Prelude
25 import Gargantext.API.Prelude (GargServerM, _ServerError)
26 import Gargantext.API.Routes (GargPrivateAPI, serverPrivateGargAPI')
27
28 class ThrowAll' e a | a -> e where
29 -- | 'throwAll' is a convenience function to throw errors across an entire
30 -- sub-API
31 --
32 --
33 -- > throwAll err400 :: Handler a :<|> Handler b :<|> Handler c
34 -- > == throwError err400 :<|> throwError err400 :<|> err400
35 throwAll' :: e -> a
36
37 instance (ThrowAll' e a, ThrowAll' e b) => ThrowAll' e (a :<|> b) where
38 throwAll' e = throwAll' e :<|> throwAll' e
39
40 -- Really this shouldn't be necessary - ((->) a) should be an instance of
41 -- MonadError, no?
42 instance {-# OVERLAPPING #-} ThrowAll' e b => ThrowAll' e (a -> b) where
43 throwAll' e = const $ throwAll' e
44
45 instance {-# OVERLAPPABLE #-} (MonadError e m) => ThrowAll' e (m a) where
46 throwAll' = throwError
47
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.