Commit 988c0f97 authored by Alfredo Di Napoli's avatar Alfredo Di Napoli

Support InternalValidationError

parent 75f7a690
...@@ -25,6 +25,7 @@ import qualified Data.Aeson as JSON ...@@ -25,6 +25,7 @@ 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) import Gargantext.Database.Query.Tree hiding (treeError)
import Data.Validity ( prettyValidation )
$(deriveHttpStatusCode ''BackendErrorCode) $(deriveHttpStatusCode ''BackendErrorCode)
...@@ -37,8 +38,11 @@ backendErrorToFrontendError = \case ...@@ -37,8 +38,11 @@ backendErrorToFrontendError = \case
-> nodeErrorToFrontendError nodeError -> nodeErrorToFrontendError nodeError
InternalTreeError treeError InternalTreeError treeError
-> treeErrorToFrontendError treeError -> treeErrorToFrontendError treeError
InternalValidationError _validationError InternalValidationError validationError
-> undefined -> mkFrontendErr' "A validation error occurred"
$ FE_validation_error $ case prettyValidation validationError of
Nothing -> "unknown_validation_error"
Just v -> T.pack v
InternalJoseError _joseError InternalJoseError _joseError
-> undefined -> undefined
InternalServerError _internalServerError InternalServerError _internalServerError
......
...@@ -45,7 +45,7 @@ import Data.Aeson.Types (typeMismatch, emptyArray) ...@@ -45,7 +45,7 @@ import Data.Aeson.Types (typeMismatch, emptyArray)
import Data.Singletons.TH import Data.Singletons.TH
import Data.List.NonEmpty (NonEmpty) import Data.List.NonEmpty (NonEmpty)
import Data.Typeable import Data.Typeable
import Data.Validity (Validation) import Data.Validity (Validation(..), ValidationChain (..), prettyValidation)
import GHC.Generics import GHC.Generics
import GHC.Stack import GHC.Stack
import Gargantext.API.Errors.Class import Gargantext.API.Errors.Class
...@@ -67,6 +67,7 @@ import qualified Gargantext.Utils.Jobs.Monad as Jobs ...@@ -67,6 +67,7 @@ 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 import qualified Data.List.NonEmpty as NE
import Data.Maybe
-- | 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
...@@ -190,6 +191,14 @@ data instance ToFrontendErrorData 'EC_404__node_error_not_found = ...@@ -190,6 +191,14 @@ data instance ToFrontendErrorData 'EC_404__node_error_not_found =
FE_node_error_not_found { nenf_node_id :: !NodeId } FE_node_error_not_found { nenf_node_id :: !NodeId }
deriving (Show, Eq, Generic) deriving (Show, Eq, Generic)
--
-- validation errors
--
data instance ToFrontendErrorData 'EC_400__validation_error =
FE_validation_error { validation_error :: T.Text }
deriving (Show, Eq, Generic)
-- --
-- Tree errors -- Tree errors
-- --
...@@ -247,6 +256,22 @@ instance FromJSON (ToFrontendErrorData 'EC_404__node_error_not_found) where ...@@ -247,6 +256,22 @@ instance FromJSON (ToFrontendErrorData 'EC_404__node_error_not_found) where
nenf_node_id <- o .: "node_id" nenf_node_id <- o .: "node_id"
pure FE_node_error_not_found{..} pure FE_node_error_not_found{..}
--
-- validation errors
--
instance ToJSON (ToFrontendErrorData 'EC_400__validation_error) where
toJSON (FE_validation_error val) = toJSON val
instance FromJSON (ToFrontendErrorData 'EC_400__validation_error) where
parseJSON (String txt) = pure $ FE_validation_error txt
parseJSON ty = typeMismatch "FE_validation_error" ty
--
-- tree errors
--
instance ToJSON (ToFrontendErrorData 'EC_404__tree_error_root_not_found) where instance ToJSON (ToFrontendErrorData 'EC_404__tree_error_root_not_found) where
toJSON _ = JSON.Null toJSON _ = JSON.Null
...@@ -292,6 +317,11 @@ genFrontendErr be = do ...@@ -292,6 +317,11 @@ genFrontendErr be = do
EC_404__node_error_not_found EC_404__node_error_not_found
-> do nodeId <- arbitrary -> do nodeId <- arbitrary
pure $ mkFrontendErr' txt (FE_node_error_not_found nodeId) pure $ mkFrontendErr' txt (FE_node_error_not_found nodeId)
-- validation error
EC_400__validation_error
-> do let genValChain = oneof [ Violated <$> arbitrary, Location <$> arbitrary <*> genValChain ]
chain <- listOf1 genValChain
pure $ mkFrontendErr' txt $ FE_validation_error (T.pack $ fromMaybe "unknown_validation_error" $ prettyValidation $ Validation chain)
-- tree errors -- tree errors
EC_404__tree_error_root_not_found EC_404__tree_error_root_not_found
...@@ -339,6 +369,11 @@ instance FromJSON FrontendError where ...@@ -339,6 +369,11 @@ instance FromJSON FrontendError where
(fe_data :: ToFrontendErrorData 'EC_500__node_error_not_implemented_yet) <- o .: "data" (fe_data :: ToFrontendErrorData 'EC_500__node_error_not_implemented_yet) <- o .: "data"
pure FrontendError{..} pure FrontendError{..}
-- validation error
EC_400__validation_error -> do
(fe_data :: ToFrontendErrorData 'EC_400__validation_error) <- 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"
......
...@@ -21,6 +21,8 @@ data BackendErrorCode ...@@ -21,6 +21,8 @@ data BackendErrorCode
| EC_404__node_error_not_found | EC_404__node_error_not_found
| EC_404__node_error_corpus_not_found | EC_404__node_error_corpus_not_found
| EC_500__node_error_not_implemented_yet | EC_500__node_error_not_implemented_yet
-- validation errors
| EC_400__validation_error
-- 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_404__tree_error_empty_root
......
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