]> Git — Sourcephile - gargantext.git/blob - src/Gargantext/Database/Query/Table/Node/Error.hs
[DB/Errors] clean error messages and structure
[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 DeriveGeneric #-}
16 {-# LANGUAGE ConstraintKinds #-}
17 {-# LANGUAGE FlexibleContexts #-}
18 {-# LANGUAGE FlexibleInstances #-}
19 {-# LANGUAGE FunctionalDependencies #-}
20 {-# LANGUAGE OverloadedStrings #-}
21 {-# LANGUAGE MultiParamTypeClasses #-}
22 {-# LANGUAGE NoImplicitPrelude #-}
23 {-# LANGUAGE RankNTypes #-}
24 {-# LANGUAGE TemplateHaskell #-}
25 {-# LANGUAGE TypeFamilies #-}
26
27 module Gargantext.Database.Query.Table.Node.Error where
28
29 import Control.Lens (Prism', (#), (^?))
30 import Control.Monad.Error.Class (MonadError(..))
31 import Gargantext.Prelude hiding (sum, head)
32 import Prelude hiding (null, id, map, sum)
33
34 ------------------------------------------------------------------------
35 data NodeError = NoListFound
36 | NoRootFound
37 | NoCorpusFound
38 | NoUserFound
39 | MkNode
40 | UserNoParent
41 | HasParent
42 | ManyParents
43 | NegativeId
44 | NotImplYet
45 | ManyNodeUsers
46
47 instance Show NodeError
48 where
49 show NoListFound = "No list found"
50 show NoRootFound = "No Root found"
51 show NoCorpusFound = "No Corpus found"
52 show NoUserFound = "No user found"
53
54 show MkNode = "Cannot make node"
55 show NegativeId = "Node with negative Id"
56 show UserNoParent = "Should not have parent"
57 show HasParent = "NodeType has parent"
58 show NotImplYet = "Not implemented yet"
59 show ManyParents = "Too many parents"
60 show ManyNodeUsers = "Many userNode/user"
61
62 class HasNodeError e where
63 _NodeError :: Prism' e NodeError
64
65 nodeError :: ( MonadError e m
66 , HasNodeError e)
67 => NodeError -> m a
68 nodeError ne = throwError $ _NodeError # ne
69
70 catchNodeError :: (MonadError e m, HasNodeError e) => m a -> (NodeError -> m a) -> m a
71 catchNodeError f g = catchError f (\e -> maybe (throwError e) g (e ^? _NodeError))