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

Merge remote-tracking branch 'origin/213-dev-implement-json-errors' into dev

parents aff1578e f3e4c0e0
......@@ -17,7 +17,8 @@ module Gargantext.API.Server where
import Control.Lens ((^.))
import Control.Monad.Except (withExceptT)
import Control.Monad.Reader (runReaderT)
import Data.Text (Text)
import qualified Data.Aeson as Aeson
import Data.Text (Text, pack)
import Data.Version (showVersion)
import Servant
import Servant.Swagger.UI (swaggerSchemaUIServer)
......@@ -63,24 +64,35 @@ server env = do
:<|> hoistServerWithContext
(Proxy :: Proxy GargAPI)
(Proxy :: Proxy AuthContext)
transform
transformJSON
(serverGargAPI (env ^. hasConfig . gc_url_backend_api))
:<|> hoistServerWithContext
(Proxy :: Proxy GraphQL.API)
(Proxy :: Proxy AuthContext)
transform
transformJSON
GraphQL.api
:<|> frontEndServer
where
transform :: forall a. GargM Env GargError a -> Handler a
transform = Handler . withExceptT showAsServantErr . (`runReaderT` env)
-- transform :: forall a. GargM Env GargError a -> Handler a
-- transform = Handler . withExceptT showAsServantErr . (`runReaderT` env)
transformJSON :: forall a. GargM Env GargError a -> Handler a
transformJSON = Handler . withExceptT showAsServantJSONErr . (`runReaderT` env)
showAsServantErr :: GargError -> ServerError
showAsServantErr (GargNodeError err@NoListFound) = err404 { errBody = BL8.pack $ show err }
showAsServantErr (GargNodeError err@(NoListFound {})) = err404 { errBody = BL8.pack $ show err }
showAsServantErr (GargNodeError err@NoRootFound) = err404 { errBody = BL8.pack $ show err }
showAsServantErr (GargNodeError err@NoCorpusFound) = err404 { errBody = BL8.pack $ show err }
showAsServantErr (GargNodeError err@NoUserFound) = err404 { errBody = BL8.pack $ show err }
showAsServantErr (GargNodeError err@(DoesNotExist _)) = err404 { errBody = BL8.pack $ show err }
showAsServantErr (GargNodeError err@(DoesNotExist {})) = err404 { errBody = BL8.pack $ show err }
showAsServantErr (GargServerError err) = err
showAsServantErr a = err500 { errBody = BL8.pack $ show a }
showAsServantJSONErr :: GargError -> ServerError
showAsServantJSONErr (GargNodeError err@(NoListFound {})) = err404 { errBody = Aeson.encode err }
showAsServantJSONErr (GargNodeError err@NoRootFound) = err404 { errBody = Aeson.encode err }
showAsServantJSONErr (GargNodeError err@NoCorpusFound) = err404 { errBody = Aeson.encode err }
showAsServantJSONErr (GargNodeError err@NoUserFound) = err404 { errBody = Aeson.encode err }
showAsServantJSONErr (GargNodeError err@(DoesNotExist {})) = err404 { errBody = Aeson.encode err }
showAsServantJSONErr (GargServerError err) = err
showAsServantJSONErr a = err500 { errBody = Aeson.encode $ Aeson.object [ ( "error", Aeson.String $ pack $ show a ) ] }
......@@ -58,7 +58,6 @@ import Gargantext.Prelude
type TableApi = Summary "Table API"
:> QueryParam "tabType" TabType
:> QueryParam "list" ListId
:> QueryParam "limit" Limit
:> QueryParam "offset" Offset
:> QueryParam "orderBy" OrderBy
......@@ -105,14 +104,13 @@ tableApi id' = getTableApi id'
getTableApi :: HasNodeError err
=> NodeId
-> Maybe TabType
-> Maybe ListId
-> Maybe Limit
-> Maybe Offset
-> Maybe OrderBy
-> Maybe Text
-> Maybe Text
-> Cmd err (HashedResponse FacetTableResult)
getTableApi cId tabType _mListId mLimit mOffset mOrderBy mQuery mYear = do
getTableApi cId tabType mLimit mOffset mOrderBy mQuery mYear = do
-- printDebug "[getTableApi] mQuery" mQuery
-- printDebug "[getTableApi] mYear" mYear
t <- getTable cId tabType mOffset mLimit mOrderBy mQuery mYear
......@@ -129,7 +127,7 @@ postTableApi cId (TableQuery o l order ft q) = case ft of
getTableHashApi :: HasNodeError err
=> NodeId -> Maybe TabType -> Cmd err Text
getTableHashApi cId tabType = do
HashedResponse { hash = h } <- getTableApi cId tabType Nothing Nothing Nothing Nothing Nothing Nothing
HashedResponse { hash = h } <- getTableApi cId tabType Nothing Nothing Nothing Nothing Nothing
pure h
searchInCorpus' :: CorpusId
......
......@@ -419,7 +419,7 @@ getOrMkList pId uId =
-- | TODO remove defaultList
defaultList :: (HasNodeError err, HasDBid NodeType) => CorpusId -> Cmd err ListId
defaultList cId =
maybe (nodeError NoListFound) (pure . view node_id) . headMay =<< getListsWithParentId cId
maybe (nodeError (NoListFound cId)) (pure . view node_id) . headMay =<< getListsWithParentId cId
defaultListMaybe :: (HasNodeError err, HasDBid NodeType) => CorpusId -> Cmd err (Maybe NodeId)
defaultListMaybe cId = headMay <$> map (view node_id ) <$> getListsWithParentId cId
......
......@@ -12,15 +12,16 @@ module Gargantext.Database.Query.Table.Node.Error where
import Control.Lens (Prism', (#), (^?))
import Control.Monad.Except (MonadError(..))
import Data.Text (Text)
import Data.Aeson
import Data.Text (Text, pack)
import Prelude hiding (null, id, map, sum)
import Gargantext.Database.Admin.Types.Node (NodeId)
import Gargantext.Database.Admin.Types.Node (ListId, NodeId(..))
import Gargantext.Prelude hiding (sum, head)
------------------------------------------------------------------------
data NodeError = NoListFound
data NodeError = NoListFound { listId :: ListId }
| NoRootFound
| NoCorpusFound
| NoUserFound
......@@ -37,7 +38,7 @@ data NodeError = NoListFound
instance Show NodeError
where
show NoListFound = "No list found"
show (NoListFound {}) = "No list found"
show NoRootFound = "No Root found"
show NoCorpusFound = "No Corpus found"
show NoUserFound = "No user found"
......@@ -53,6 +54,13 @@ instance Show NodeError
show NeedsConfiguration = "Needs configuration"
show (NodeError e) = "NodeError: " <> cs e
instance ToJSON NodeError where
toJSON (NoListFound { listId = NodeId listId }) =
object [ ( "error", "No list found" )
, ( "listId", Number $ fromIntegral listId ) ]
toJSON err =
object [ ( "error", String $ pack $ show err ) ]
class HasNodeError e where
_NodeError :: Prism' e NodeError
......
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