2 Module : Gargantext.API.Types
3 Description : Server API main Types
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
13 {-# LANGUAGE ConstraintKinds #-}
14 {-# LANGUAGE FlexibleContexts #-}
15 {-# LANGUAGE FlexibleInstances #-}
16 {-# LANGUAGE FunctionalDependencies #-}
17 {-# LANGUAGE MultiParamTypeClasses #-}
18 {-# LANGUAGE NoImplicitPrelude #-}
19 {-# LANGUAGE OverloadedStrings #-}
20 {-# LANGUAGE RankNTypes #-}
21 {-# LANGUAGE TemplateHaskell #-}
22 {-# LANGUAGE TypeOperators #-}
23 {-# LANGUAGE UndecidableInstances #-}
25 module Gargantext.API.Types
26 ( module Gargantext.API.Types
32 import Control.Exception (Exception)
33 import Control.Lens (Prism', (#))
34 import Control.Lens.TH (makePrisms)
35 import Control.Monad.Error.Class (MonadError(throwError))
36 import Crypto.JOSE.Error as Jose
37 import Data.Aeson.Types
41 import Servant.Job.Core (HasServerError(..), serverError)
42 import Servant.Job.Async (HasJobEnv)
43 import Gargantext.Prelude
44 import Gargantext.API.Settings
45 import Gargantext.API.Orchestrator.Types
46 import Gargantext.API.Ngrams
47 import Gargantext.Core.Types
48 import Gargantext.Database.Tree
49 import Gargantext.Database.Utils
50 import Gargantext.Database.Schema.Node
52 class HasJoseError e where
53 _JoseError :: Prism' e Jose.Error
55 joseError :: (MonadError e m, HasJoseError e) => Jose.Error -> m a
56 joseError = throwError . (_JoseError #)
58 class ThrowAll' e a | a -> e where
59 -- | 'throwAll' is a convenience function to throw errors across an entire
63 -- > throwAll err400 :: Handler a :<|> Handler b :<|> Handler c
64 -- > == throwError err400 :<|> throwError err400 :<|> err400
67 instance (ThrowAll' e a, ThrowAll' e b) => ThrowAll' e (a :<|> b) where
68 throwAll' e = throwAll' e :<|> throwAll' e
70 -- Really this shouldn't be necessary - ((->) a) should be an instance of
72 instance {-# OVERLAPPING #-} ThrowAll' e b => ThrowAll' e (a -> b) where
73 throwAll' e = const $ throwAll' e
75 instance {-# OVERLAPPABLE #-} (MonadError e m) => ThrowAll' e (m a) where
76 throwAll' = throwError
78 type GargServerC env err m =
85 , ToJSON err -- TODO this is arguable
89 , HasJobEnv env ScraperStatus ScraperStatus
92 type GargServerT env err m api = GargServerC env err m => ServerT api m
95 forall env err m. GargServerT env err m api
98 = GargNodeError NodeError
99 | GargTreeError TreeError
100 | GargInvalidError Validation
101 | GargJoseError Jose.Error
102 | GargServerError ServerError
103 deriving (Show, Typeable)
105 makePrisms ''GargError
107 instance ToJSON GargError where
108 toJSON _ = String "SomeGargErrorPleaseReport"
110 instance Exception GargError
112 instance HasNodeError GargError where
113 _NodeError = _GargNodeError
115 instance HasInvalidError GargError where
116 _InvalidError = _GargInvalidError
118 instance HasTreeError GargError where
119 _TreeError = _GargTreeError
121 instance HasServerError GargError where
122 _ServerError = _GargServerError
124 instance HasJoseError GargError where
125 _JoseError = _GargJoseError