2 Module : Gargantext.Database.Types.Error
4 Copyright : (c) CNRS, 2017-Present
5 License : AGPL + CECILL v3
6 Maintainer : team@gargantext.org
7 Stability : experimental
11 module Gargantext.Database.Query.Table.Node.Error where
13 import Control.Lens (Prism', (#), (^?))
14 import Control.Monad.Except (MonadError(..))
16 import Data.Text (Text, pack)
18 import Prelude hiding (null, id, map, sum)
20 import Gargantext.Database.Admin.Types.Node (ListId, NodeId(..))
21 import Gargantext.Prelude hiding (sum, head)
23 ------------------------------------------------------------------------
24 data NodeError = NoListFound { listId :: ListId }
39 instance Show NodeError
41 show (NoListFound {}) = "No list found"
42 show NoRootFound = "No Root found"
43 show NoCorpusFound = "No Corpus found"
44 show NoUserFound = "No user found"
46 show MkNode = "Cannot make node"
47 show NegativeId = "Node with negative Id"
48 show UserNoParent = "Should not have parent"
49 show HasParent = "NodeType has parent"
50 show NotImplYet = "Not implemented yet"
51 show ManyParents = "Too many parents"
52 show ManyNodeUsers = "Many userNode/user"
53 show (DoesNotExist n) = "Node does not exist (" <> show n <> ")"
54 show NeedsConfiguration = "Needs configuration"
55 show (NodeError e) = "NodeError: " <> cs e
57 instance ToJSON NodeError where
58 toJSON (NoListFound { listId = NodeId listId }) =
59 object [ ( "error", "No list found" )
60 , ( "listId", Number $ fromIntegral listId ) ]
62 object [ ( "error", String $ pack $ show err ) ]
64 class HasNodeError e where
65 _NodeError :: Prism' e NodeError
67 errorWith :: ( MonadError e m
70 errorWith x = nodeError (NodeError x)
72 nodeError :: ( MonadError e m
75 nodeError ne = throwError $ _NodeError # ne
77 catchNodeError :: (MonadError e m, HasNodeError e) => m a -> (NodeError -> m a) -> m a
78 catchNodeError f g = catchError f (\e -> maybe (throwError e) g (e ^? _NodeError))