]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Database/Query/Table/Node/Error.hs
[FIX] strict type
[gargantext.git] / src / Gargantext / Database / Query / Table / Node / Error.hs
1 {-|
2 Module : Gargantext.Database.Types.Error
3 Description :
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 {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
12 {-# OPTIONS_GHC -fno-warn-orphans #-}
13
14 {-# LANGUAGE Arrows #-}
15 {-# LANGUAGE ConstraintKinds #-}
16 {-# LANGUAGE FunctionalDependencies #-}
17 {-# LANGUAGE TemplateHaskell #-}
18 {-# LANGUAGE TypeFamilies #-}
19
20 module Gargantext.Database.Query.Table.Node.Error where
21
22 import Gargantext.Database.Admin.Types.Node (NodeId)
23 import Control.Lens (Prism', (#), (^?))
24 import Control.Monad.Error.Class (MonadError(..))
25 import Gargantext.Prelude hiding (sum, head)
26 import Prelude hiding (null, id, map, sum)
27
28 ------------------------------------------------------------------------
29 data NodeError = NoListFound
30 | NoRootFound
31 | NoCorpusFound
32 | NoUserFound
33 | MkNode
34 | UserNoParent
35 | HasParent
36 | ManyParents
37 | NegativeId
38 | NotImplYet
39 | ManyNodeUsers
40 | DoesNotExist NodeId
41
42 instance Show NodeError
43 where
44 show NoListFound = "No list found"
45 show NoRootFound = "No Root found"
46 show NoCorpusFound = "No Corpus found"
47 show NoUserFound = "No user found"
48
49 show MkNode = "Cannot make node"
50 show NegativeId = "Node with negative Id"
51 show UserNoParent = "Should not have parent"
52 show HasParent = "NodeType has parent"
53 show NotImplYet = "Not implemented yet"
54 show ManyParents = "Too many parents"
55 show ManyNodeUsers = "Many userNode/user"
56 show (DoesNotExist n) = "Node does not exist" <> show n
57
58 class HasNodeError e where
59 _NodeError :: Prism' e NodeError
60
61 nodeError :: ( MonadError e m
62 , HasNodeError e)
63 => NodeError -> m a
64 nodeError ne = throwError $ _NodeError # ne
65
66 catchNodeError :: (MonadError e m, HasNodeError e) => m a -> (NodeError -> m a) -> m a
67 catchNodeError f g = catchError f (\e -> maybe (throwError e) g (e ^? _NodeError))