Commit b5d6e997 authored by Alexandre Delanoë's avatar Alexandre Delanoë

[ERROR] Handling.

parent bbe66c85
Pipeline #91 canceled with stage
...@@ -35,12 +35,10 @@ module Gargantext.API.Node ...@@ -35,12 +35,10 @@ module Gargantext.API.Node
------------------------------------------------------------------- -------------------------------------------------------------------
import Control.Lens (prism', set) import Control.Lens (prism', set)
import Control.Monad.IO.Class (liftIO) import Control.Monad.IO.Class (liftIO)
import Control.Monad ((>>), guard) import Control.Monad ((>>))
--import System.IO (putStrLn, readFile) --import System.IO (putStrLn, readFile)
import Data.Aeson (FromJSON, ToJSON) import Data.Aeson (FromJSON, ToJSON)
import Data.Functor (($>))
--import Data.Text (Text(), pack)
import Data.Text (Text()) import Data.Text (Text())
import Data.Swagger import Data.Swagger
import Data.Time (UTCTime) import Data.Time (UTCTime)
...@@ -269,11 +267,10 @@ graphAPI nId = do ...@@ -269,11 +267,10 @@ graphAPI nId = do
-- TODO what do we get about the node? to replace contextText -- TODO what do we get about the node? to replace contextText
instance HasNodeError ServantErr where instance HasNodeError ServantErr where
_NodeError = prism' make match _NodeError = prism' mk (const $ panic "HasNodeError ServantErr: not a prism")
where where
err = err404 { errBody = "NodeError: No list found" } mk NoListFound = err404 { errBody = "NodeError: No list found" }
make NoListFound = err mk MkNodeError = err404 { errBody = "NodeError: Cannot mk node" }
match e = guard (e == err) $> NoListFound
-- TODO(orphan): There should be a proper APIError data type with a case TreeError. -- TODO(orphan): There should be a proper APIError data type with a case TreeError.
instance HasTreeError ServantErr where instance HasTreeError ServantErr where
......
...@@ -60,7 +60,7 @@ flowCorpus ff fp cName = do ...@@ -60,7 +60,7 @@ flowCorpus ff fp cName = do
flowCorpus' NodeCorpus hyperdataDocuments' params flowCorpus' NodeCorpus hyperdataDocuments' params
flowInsert :: NodeType -> [HyperdataDocument] -> CorpusName flowInsert :: HasNodeError err => NodeType -> [HyperdataDocument] -> CorpusName
-> Cmd err ([ReturnId], MasterUserId, MasterCorpusId, UserId, CorpusId) -> Cmd err ([ReturnId], MasterUserId, MasterCorpusId, UserId, CorpusId)
flowInsert _nt hyperdataDocuments cName = do flowInsert _nt hyperdataDocuments cName = do
let hyperdataDocuments' = map (\h -> ToDbDocument h) hyperdataDocuments let hyperdataDocuments' = map (\h -> ToDbDocument h) hyperdataDocuments
...@@ -74,14 +74,14 @@ flowInsert _nt hyperdataDocuments cName = do ...@@ -74,14 +74,14 @@ flowInsert _nt hyperdataDocuments cName = do
pure (ids, masterUserId, masterCorpusId, userId, userCorpusId) pure (ids, masterUserId, masterCorpusId, userId, userCorpusId)
flowAnnuaire :: FilePath -> Cmd err () flowAnnuaire :: HasNodeError err => FilePath -> Cmd err ()
flowAnnuaire filePath = do flowAnnuaire filePath = do
contacts <- liftIO $ deserialiseImtUsersFromFile filePath contacts <- liftIO $ deserialiseImtUsersFromFile filePath
ps <- flowInsertAnnuaire "Annuaire" $ map (\h-> ToDbContact h) $ map addUniqIdsContact contacts ps <- flowInsertAnnuaire "Annuaire" $ map (\h-> ToDbContact h) $ map addUniqIdsContact contacts
printDebug "length annuaire" ps printDebug "length annuaire" ps
flowInsertAnnuaire :: CorpusName -> [ToDbData] flowInsertAnnuaire :: HasNodeError err => CorpusName -> [ToDbData]
-> Cmd err ([ReturnId], UserId, CorpusId, UserId, CorpusId) -> Cmd err ([ReturnId], UserId, CorpusId, UserId, CorpusId)
flowInsertAnnuaire name children = do flowInsertAnnuaire name children = do
...@@ -136,7 +136,7 @@ flowCorpus' _ _ _ = undefined ...@@ -136,7 +136,7 @@ flowCorpus' _ _ _ = undefined
type CorpusName = Text type CorpusName = Text
subFlowCorpus :: Username -> CorpusName -> Cmd err (UserId, RootId, CorpusId) subFlowCorpus :: HasNodeError err => Username -> CorpusName -> Cmd err (UserId, RootId, CorpusId)
subFlowCorpus username cName = do subFlowCorpus username cName = do
maybeUserId <- getUser username maybeUserId <- getUser username
...@@ -172,7 +172,7 @@ subFlowCorpus username cName = do ...@@ -172,7 +172,7 @@ subFlowCorpus username cName = do
pure (userId, rootId, corpusId) pure (userId, rootId, corpusId)
subFlowAnnuaire :: Username -> CorpusName -> Cmd err (UserId, RootId, CorpusId) subFlowAnnuaire :: HasNodeError err => Username -> CorpusName -> Cmd err (UserId, RootId, CorpusId)
subFlowAnnuaire username _cName = do subFlowAnnuaire username _cName = do
maybeUserId <- getUser username maybeUserId <- getUser username
...@@ -228,7 +228,7 @@ data DocumentIdWithNgrams = ...@@ -228,7 +228,7 @@ data DocumentIdWithNgrams =
} deriving (Show) } deriving (Show)
-- TODO group terms -- TODO group terms
extractNgramsT :: HyperdataDocument -> Cmd err (Map (NgramsT Ngrams) Int) extractNgramsT :: HasNodeError err => HyperdataDocument -> Cmd err (Map (NgramsT Ngrams) Int)
extractNgramsT doc = do extractNgramsT doc = do
let source = text2ngrams $ maybe "Nothing" identity $ _hyperdataDocument_source doc let source = text2ngrams $ maybe "Nothing" identity $ _hyperdataDocument_source doc
...@@ -244,7 +244,7 @@ extractNgramsT doc = do ...@@ -244,7 +244,7 @@ extractNgramsT doc = do
documentIdWithNgrams :: (HyperdataDocument -> Cmd err (Map (NgramsT Ngrams) Int)) documentIdWithNgrams :: HasNodeError err => (HyperdataDocument -> Cmd err (Map (NgramsT Ngrams) Int))
-> [DocumentWithId] -> Cmd err [DocumentIdWithNgrams] -> [DocumentWithId] -> Cmd err [DocumentIdWithNgrams]
documentIdWithNgrams f = mapM toDocumentIdWithNgrams documentIdWithNgrams f = mapM toDocumentIdWithNgrams
where where
...@@ -259,7 +259,7 @@ mapNodeIdNgrams ds = DM.map (DM.fromListWith (+)) $ DM.fromListWith (<>) xs ...@@ -259,7 +259,7 @@ mapNodeIdNgrams ds = DM.map (DM.fromListWith (+)) $ DM.fromListWith (<>) xs
xs = [(ng, [(nId, i)]) | (nId, n2i') <- n2i ds, (ng, i) <- DM.toList n2i'] xs = [(ng, [(nId, i)]) | (nId, n2i') <- n2i ds, (ng, i) <- DM.toList n2i']
n2i = map (\d -> ((documentId . documentWithId) d, document_ngrams d)) n2i = map (\d -> ((documentId . documentWithId) d, document_ngrams d))
indexNgrams :: Map (NgramsT Ngrams ) (Map NodeId Int) indexNgrams :: HasNodeError err => Map (NgramsT Ngrams ) (Map NodeId Int)
-> Cmd err (Map (NgramsT NgramsIndexed) (Map NodeId Int)) -> Cmd err (Map (NgramsT NgramsIndexed) (Map NodeId Int))
indexNgrams ng2nId = do indexNgrams ng2nId = do
terms2id <- insertNgrams (map _ngramsT $ DM.keys ng2nId) terms2id <- insertNgrams (map _ngramsT $ DM.keys ng2nId)
...@@ -297,7 +297,7 @@ groupNgramsBy isEqual cId = DM.fromList $ catMaybes [ isEqual n1 n2 | n1 <- DM.k ...@@ -297,7 +297,7 @@ groupNgramsBy isEqual cId = DM.fromList $ catMaybes [ isEqual n1 n2 | n1 <- DM.k
-- TODO check: do not insert duplicates -- TODO check: do not insert duplicates
insertGroups :: ListId -> Map NgramsIndexed NgramsIndexed -> Cmd err Int insertGroups :: HasNodeError err => ListId -> Map NgramsIndexed NgramsIndexed -> Cmd err Int
insertGroups lId ngrs = insertGroups lId ngrs =
insertNodeNgramsNgramsNew [ NodeNgramsNgrams lId ng1 ng2 (Just 1) insertNodeNgramsNgramsNew [ NodeNgramsNgrams lId ng1 ng2 (Just 1)
| (ng1, ng2) <- map (both _ngramsId) $ DM.toList ngrs | (ng1, ng2) <- map (both _ngramsId) $ DM.toList ngrs
...@@ -310,7 +310,7 @@ ngrams2list :: Map (NgramsT NgramsIndexed) (Map NodeId Int) -> [(ListType,Ngrams ...@@ -310,7 +310,7 @@ ngrams2list :: Map (NgramsT NgramsIndexed) (Map NodeId Int) -> [(ListType,Ngrams
ngrams2list = zip (repeat CandidateList) . map (\(NgramsT _lost_t ng) -> ng) . DM.keys ngrams2list = zip (repeat CandidateList) . map (\(NgramsT _lost_t ng) -> ng) . DM.keys
-- | TODO: weight of the list could be a probability -- | TODO: weight of the list could be a probability
insertLists :: ListId -> [(ListType,NgramsIndexed)] -> Cmd err Int insertLists :: HasNodeError err => ListId -> [(ListType,NgramsIndexed)] -> Cmd err Int
insertLists lId lngs = insertLists lId lngs =
insertNodeNgrams [ NodeNgram Nothing lId ngr (fromIntegral $ listTypeId l) (listTypeId l) insertNodeNgrams [ NodeNgram Nothing lId ngr (fromIntegral $ listTypeId l) (listTypeId l)
| (l,ngr) <- map (second _ngramsId) lngs | (l,ngr) <- map (second _ngramsId) lngs
......
...@@ -50,7 +50,7 @@ import Prelude hiding (null, id, map, sum) ...@@ -50,7 +50,7 @@ import Prelude hiding (null, id, map, sum)
------------------------------------------------------------------------ ------------------------------------------------------------------------
data NodeError = NoListFound data NodeError = NoListFound | MkNodeError
deriving (Show) deriving (Show)
class HasNodeError e where class HasNodeError e where
...@@ -518,7 +518,7 @@ getOrMkList :: HasNodeError err => ParentId -> UserId -> Cmd err Int ...@@ -518,7 +518,7 @@ getOrMkList :: HasNodeError err => ParentId -> UserId -> Cmd err Int
getOrMkList pId uId = getOrMkList pId uId =
defaultList pId defaultList pId
`catchNodeError` `catchNodeError`
(\NoListFound -> maybe (nodeError NoListFound) pure . headMay =<< mkList pId uId) (\_ -> maybe (nodeError MkNodeError) pure . headMay =<< mkList pId uId)
defaultList :: HasNodeError err => CorpusId -> Cmd err ListId defaultList :: HasNodeError err => CorpusId -> Cmd err ListId
defaultList cId = defaultList cId =
......
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