Utils.purs 1.98 KB
Newer Older
1 2 3 4 5 6
module Gargantext.Config.Utils where

import Gargantext.Prelude

import Data.Array as A
import Data.Either (Either(..))
7
import Data.Foldable (foldl)
8
import Data.Maybe (fromMaybe)
9
import Effect (Effect)
10 11
import Effect.Aff (Aff)
import Effect.Class (liftEffect)
12
import Gargantext.Config.REST (RESTError, logRESTError)
13
import Gargantext.Types (AsyncEvent(..), AsyncProgress(..), AsyncTaskLog(..), AsyncTaskStatus(..), FrontendError(..))
14 15 16 17 18 19
import Gargantext.Utils.Reactix as R2
import Toestand as T

here :: R2.Here
here = R2.here "Gargantext.Config.Utils"

20
handleRESTError :: forall a.
21 22
                   R2.Here
                -> T.Box (Array FrontendError)
23 24 25
                -> Either RESTError a
                -> (a -> Aff Unit)
                -> Aff Unit
26
handleRESTError here' errors (Left error) _ = liftEffect $ do
27
  T.modify_ (A.cons $ FRESTError { error }) errors
28 29 30
  logRESTError here' "[handleTaskError]" error
  -- here.warn2 "[handleTaskError] RESTError" error
handleRESTError _ _ (Right task) handler = handler task
31 32 33 34

handleErrorInAsyncProgress :: T.Box (Array FrontendError)
                           -> AsyncProgress
                           -> Effect Unit
35 36
handleErrorInAsyncProgress errors ap@(AsyncProgress { status: IsFailure }) = do
  T.modify_ (A.cons $ FStringError { error: concatErrors ap }) errors
37 38 39 40 41 42 43
handleErrorInAsyncProgress errors ap@(AsyncProgress { log, status: IsFinished }) = do
  if countFailed > 0 then
    T.modify_ (A.cons $ FStringError { error: concatErrors ap }) errors
  else
    pure unit
  where
    countFailed = foldl (+) 0 $ (\(AsyncTaskLog { failed }) -> failed) <$> log
44 45 46
handleErrorInAsyncProgress _ _ = pure unit

concatErrors :: AsyncProgress -> String
47
concatErrors (AsyncProgress { error, log }) = foldl eventsErrorMessage (fromMaybe "" error) log
48 49 50 51
  where
    eventsErrorMessage acc (AsyncTaskLog { events }) = (foldl eventErrorMessage "" events) <> "\n" <> acc
    eventErrorMessage acc (AsyncEvent { level: "ERROR", message }) = message <> "\n" <> acc
    eventErrorMessage acc _ = acc