1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
{-|
Module : Gargantext.Database.Types.Error
Description :
Copyright : (c) CNRS, 2017-Present
License : AGPL + CECILL v3
Maintainer : team@gargantext.org
Stability : experimental
Portability : POSIX
-}
module Gargantext.Database.Query.Table.Node.Error where
import Control.Lens (Prism', (#), (^?))
import Control.Monad.Except (MonadError(..))
import Data.Text (Text)
import Prelude hiding (null, id, map, sum)
import Gargantext.Database.Admin.Types.Node (NodeId)
import Gargantext.Prelude hiding (sum, head)
------------------------------------------------------------------------
data NodeError = NoListFound
| NoRootFound
| NoCorpusFound
| NoUserFound
| MkNode
| UserNoParent
| HasParent
| ManyParents
| NegativeId
| NotImplYet
| ManyNodeUsers
| DoesNotExist NodeId
| NeedsConfiguration
| NodeError Text
instance Show NodeError
where
show NoListFound = "No list found"
show NoRootFound = "No Root found"
show NoCorpusFound = "No Corpus found"
show NoUserFound = "No user found"
show MkNode = "Cannot make node"
show NegativeId = "Node with negative Id"
show UserNoParent = "Should not have parent"
show HasParent = "NodeType has parent"
show NotImplYet = "Not implemented yet"
show ManyParents = "Too many parents"
show ManyNodeUsers = "Many userNode/user"
show (DoesNotExist n) = "Node does not exist (" <> show n <> ")"
show NeedsConfiguration = "Needs configuration"
show (NodeError e) = "NodeError: " <> cs e
class HasNodeError e where
_NodeError :: Prism' e NodeError
errorWith :: ( MonadError e m
, HasNodeError e)
=> Text -> m a
errorWith x = nodeError (NodeError x)
nodeError :: ( MonadError e m
, HasNodeError e)
=> NodeError -> m a
nodeError ne = throwError $ _NodeError # ne
catchNodeError :: (MonadError e m, HasNodeError e) => m a -> (NodeError -> m a) -> m a
catchNodeError f g = catchError f (\e -> maybe (throwError e) g (e ^? _NodeError))