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