Commit 75f7a690 authored by Alfredo Di Napoli's avatar Alfredo Di Napoli

Add treeErrorToFrontendError

parent b44e4c16
...@@ -24,6 +24,7 @@ import Servant.Server ...@@ -24,6 +24,7 @@ import Servant.Server
import qualified Data.Aeson as JSON import qualified Data.Aeson as JSON
import qualified Network.HTTP.Types.Status as HTTP import qualified Network.HTTP.Types.Status as HTTP
import qualified Data.Text as T import qualified Data.Text as T
import Gargantext.Database.Query.Tree hiding (treeError)
$(deriveHttpStatusCode ''BackendErrorCode) $(deriveHttpStatusCode ''BackendErrorCode)
...@@ -34,8 +35,8 @@ backendErrorToFrontendError :: BackendInternalError -> FrontendError ...@@ -34,8 +35,8 @@ backendErrorToFrontendError :: BackendInternalError -> FrontendError
backendErrorToFrontendError = \case backendErrorToFrontendError = \case
InternalNodeError nodeError InternalNodeError nodeError
-> nodeErrorToFrontendError nodeError -> nodeErrorToFrontendError nodeError
InternalTreeError _treeError InternalTreeError treeError
-> undefined -> treeErrorToFrontendError treeError
InternalValidationError _validationError InternalValidationError _validationError
-> undefined -> undefined
InternalJoseError _joseError InternalJoseError _joseError
...@@ -80,6 +81,12 @@ nodeErrorToFrontendError ne = case ne of ...@@ -80,6 +81,12 @@ nodeErrorToFrontendError ne = case ne of
QueryNoParse _txt QueryNoParse _txt
-> undefined -> undefined
treeErrorToFrontendError :: TreeError -> FrontendError
treeErrorToFrontendError te = case te of
NoRoot -> mkFrontendErrShow FE_tree_error_root_not_found
EmptyRoot -> mkFrontendErrShow FE_tree_error_empty_root
TooManyRoots roots -> mkFrontendErrShow $ FE_tree_error_too_many_roots roots
-- | Converts a 'FrontendError' into a 'ServerError' that the servant app can -- | Converts a 'FrontendError' into a 'ServerError' that the servant app can
-- return to the frontend. -- return to the frontend.
frontendErrorToServerError :: FrontendError -> ServerError frontendErrorToServerError :: FrontendError -> ServerError
......
...@@ -43,6 +43,7 @@ import Control.Lens (makePrisms) ...@@ -43,6 +43,7 @@ import Control.Lens (makePrisms)
import Data.Aeson as JSON import Data.Aeson as JSON
import Data.Aeson.Types (typeMismatch, emptyArray) import Data.Aeson.Types (typeMismatch, emptyArray)
import Data.Singletons.TH import Data.Singletons.TH
import Data.List.NonEmpty (NonEmpty)
import Data.Typeable import Data.Typeable
import Data.Validity (Validation) import Data.Validity (Validation)
import GHC.Generics import GHC.Generics
...@@ -65,6 +66,7 @@ import qualified Data.Text as T ...@@ -65,6 +66,7 @@ import qualified Data.Text as T
import qualified Gargantext.Utils.Jobs.Monad as Jobs import qualified Gargantext.Utils.Jobs.Monad as Jobs
import qualified Servant.Job.Types as SJ import qualified Servant.Job.Types as SJ
import Text.Read (readMaybe) import Text.Read (readMaybe)
import qualified Data.List.NonEmpty as NE
-- | A 'WithStacktrace' carries an error alongside its -- | A 'WithStacktrace' carries an error alongside its
-- 'CallStack', to be able to print the correct source location -- 'CallStack', to be able to print the correct source location
...@@ -193,7 +195,15 @@ data instance ToFrontendErrorData 'EC_404__node_error_not_found = ...@@ -193,7 +195,15 @@ data instance ToFrontendErrorData 'EC_404__node_error_not_found =
-- --
data instance ToFrontendErrorData 'EC_404__tree_error_root_not_found = data instance ToFrontendErrorData 'EC_404__tree_error_root_not_found =
RootNotFound { _rnf_rootId :: RootId } FE_tree_error_root_not_found
deriving (Show, Eq, Generic)
data instance ToFrontendErrorData 'EC_404__tree_error_empty_root =
FE_tree_error_empty_root
deriving (Show, Eq, Generic)
data instance ToFrontendErrorData 'EC_500__tree_error_too_many_roots =
FE_tree_error_too_many_roots { tmr_roots :: NonEmpty NodeId }
deriving (Show, Eq, Generic) deriving (Show, Eq, Generic)
---------------------------------------------------------------------------- ----------------------------------------------------------------------------
...@@ -238,12 +248,25 @@ instance FromJSON (ToFrontendErrorData 'EC_404__node_error_not_found) where ...@@ -238,12 +248,25 @@ instance FromJSON (ToFrontendErrorData 'EC_404__node_error_not_found) where
pure FE_node_error_not_found{..} pure FE_node_error_not_found{..}
instance ToJSON (ToFrontendErrorData 'EC_404__tree_error_root_not_found) where instance ToJSON (ToFrontendErrorData 'EC_404__tree_error_root_not_found) where
toJSON RootNotFound{..} = object [ "root_id" .= toJSON _rnf_rootId ] toJSON _ = JSON.Null
instance FromJSON (ToFrontendErrorData 'EC_404__tree_error_root_not_found) where instance FromJSON (ToFrontendErrorData 'EC_404__tree_error_root_not_found) where
parseJSON = withObject "RootNotFound" $ \o -> do parseJSON _ = pure FE_tree_error_root_not_found
_rnf_rootId <- o .: "root_id"
pure RootNotFound{..} instance ToJSON (ToFrontendErrorData 'EC_404__tree_error_empty_root) where
toJSON _ = JSON.Null
instance FromJSON (ToFrontendErrorData 'EC_404__tree_error_empty_root) where
parseJSON _ = pure FE_tree_error_empty_root
instance ToJSON (ToFrontendErrorData 'EC_500__tree_error_too_many_roots) where
toJSON (FE_tree_error_too_many_roots roots) =
object [ "node_ids" .= NE.toList roots ]
instance FromJSON (ToFrontendErrorData 'EC_500__tree_error_too_many_roots) where
parseJSON = withObject "FE_tree_error_too_many_roots" $ \o -> do
tmr_roots <- o .: "node_ids"
pure FE_tree_error_too_many_roots{..}
---------------------------------------------------------------------------- ----------------------------------------------------------------------------
-- Arbitrary instances and test data generation -- Arbitrary instances and test data generation
...@@ -272,8 +295,12 @@ genFrontendErr be = do ...@@ -272,8 +295,12 @@ genFrontendErr be = do
-- tree errors -- tree errors
EC_404__tree_error_root_not_found EC_404__tree_error_root_not_found
-> do rootId <- arbitrary -> pure $ mkFrontendErr' txt $ FE_tree_error_root_not_found
pure $ mkFrontendErr' txt (RootNotFound rootId) EC_404__tree_error_empty_root
-> pure $ mkFrontendErr' txt $ FE_tree_error_empty_root
EC_500__tree_error_too_many_roots
-> do nodes <- arbitrary
pure $ mkFrontendErr' txt $ FE_tree_error_too_many_roots nodes
instance ToJSON BackendErrorCode where instance ToJSON BackendErrorCode where
toJSON = JSON.String . T.pack . drop 3 . show toJSON = JSON.String . T.pack . drop 3 . show
...@@ -308,11 +335,17 @@ instance FromJSON FrontendError where ...@@ -308,11 +335,17 @@ instance FromJSON FrontendError where
EC_404__node_error_not_found -> do EC_404__node_error_not_found -> do
(fe_data :: ToFrontendErrorData 'EC_404__node_error_not_found) <- o .: "data" (fe_data :: ToFrontendErrorData 'EC_404__node_error_not_found) <- o .: "data"
pure FrontendError{..} pure FrontendError{..}
EC_500__node_error_not_implemented_yet -> do
(fe_data :: ToFrontendErrorData 'EC_500__node_error_not_implemented_yet) <- o .: "data"
pure FrontendError{..}
-- tree errors -- tree errors
EC_404__tree_error_root_not_found -> do EC_404__tree_error_root_not_found -> do
(fe_data :: ToFrontendErrorData 'EC_404__tree_error_root_not_found) <- o .: "data" (fe_data :: ToFrontendErrorData 'EC_404__tree_error_root_not_found) <- o .: "data"
pure FrontendError{..} pure FrontendError{..}
EC_500__node_error_not_implemented_yet -> do EC_404__tree_error_empty_root -> do
(fe_data :: ToFrontendErrorData 'EC_500__node_error_not_implemented_yet) <- o .: "data" (fe_data :: ToFrontendErrorData 'EC_404__tree_error_empty_root) <- o .: "data"
pure FrontendError{..}
EC_500__tree_error_too_many_roots -> do
(fe_data :: ToFrontendErrorData 'EC_500__tree_error_too_many_roots) <- o .: "data"
pure FrontendError{..} pure FrontendError{..}
...@@ -23,6 +23,8 @@ data BackendErrorCode ...@@ -23,6 +23,8 @@ data BackendErrorCode
| EC_500__node_error_not_implemented_yet | EC_500__node_error_not_implemented_yet
-- tree errors -- tree errors
| EC_404__tree_error_root_not_found | EC_404__tree_error_root_not_found
| EC_404__tree_error_empty_root
| EC_500__tree_error_too_many_roots
deriving (Show, Read, Eq, Enum, Bounded) deriving (Show, Read, Eq, Enum, Bounded)
$(genSingletons [''BackendErrorCode]) $(genSingletons [''BackendErrorCode])
......
...@@ -64,6 +64,7 @@ import Gargantext.Database.Query.Tree.Error ...@@ -64,6 +64,7 @@ import Gargantext.Database.Query.Tree.Error
import Gargantext.Database.Schema.Node (NodePoly(..)) import Gargantext.Database.Schema.Node (NodePoly(..))
import Gargantext.Database.Schema.NodeNode (NodeNodePoly(..)) import Gargantext.Database.Schema.NodeNode (NodeNodePoly(..))
import Gargantext.Prelude hiding (to) import Gargantext.Prelude hiding (to)
import qualified Data.List.NonEmpty as NE
------------------------------------------------------------------------ ------------------------------------------------------------------------
data DbTreeNode = DbTreeNode { _dt_nodeId :: NodeId data DbTreeNode = DbTreeNode { _dt_nodeId :: NodeId
...@@ -254,6 +255,9 @@ findNodesWithType root target through = ...@@ -254,6 +255,9 @@ findNodesWithType root target through =
isInTarget n = List.elem (fromDBid $ view dt_typeId n) isInTarget n = List.elem (fromDBid $ view dt_typeId n)
$ List.nub $ target <> through $ List.nub $ target <> through
treeNodeToNodeId :: DbTreeNode -> NodeId
treeNodeToNodeId = _dt_nodeId
------------------------------------------------------------------------ ------------------------------------------------------------------------
------------------------------------------------------------------------ ------------------------------------------------------------------------
toTree :: ( MonadError e m toTree :: ( MonadError e m
...@@ -266,7 +270,7 @@ toTree m = ...@@ -266,7 +270,7 @@ toTree m =
Just [root] -> pure $ toTree' m root Just [root] -> pure $ toTree' m root
Nothing -> treeError NoRoot Nothing -> treeError NoRoot
Just [] -> treeError EmptyRoot Just [] -> treeError EmptyRoot
Just _r -> treeError TooManyRoots Just r -> treeError $ TooManyRoots (NE.fromList $ map treeNodeToNodeId r)
where where
toTree' :: Map (Maybe ParentId) [DbTreeNode] toTree' :: Map (Maybe ParentId) [DbTreeNode]
......
...@@ -15,19 +15,22 @@ module Gargantext.Database.Query.Tree.Error ...@@ -15,19 +15,22 @@ module Gargantext.Database.Query.Tree.Error
where where
import Control.Lens (Prism', (#)) import Control.Lens (Prism', (#))
import Gargantext.Core.Types
import Gargantext.Prelude import Gargantext.Prelude
import Prelude qualified import Prelude qualified
import qualified Data.List.NonEmpty as NE
import qualified Data.Text as T
------------------------------------------------------------------------ ------------------------------------------------------------------------
data TreeError = NoRoot data TreeError = NoRoot
| EmptyRoot | EmptyRoot
| TooManyRoots | TooManyRoots (NonEmpty NodeId)
instance Prelude.Show TreeError instance Prelude.Show TreeError
where where
show NoRoot = "Root node not found" show NoRoot = "Root node not found"
show EmptyRoot = "Root node should not be empty" show EmptyRoot = "Root node should not be empty"
show TooManyRoots = "Too many root nodes" show (TooManyRoots roots) = "Too many root nodes: [" <> T.unpack (T.intercalate "," . map show $ NE.toList roots) <> "]"
class HasTreeError e where class HasTreeError e where
_TreeError :: Prism' e TreeError _TreeError :: Prism' e TreeError
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment