Add singleError utility function

This commit is contained in:
2019-07-23 06:04:33 +02:00
parent 1b5094b6a3
commit 282946560e
6 changed files with 64 additions and 15 deletions

View File

@ -7,6 +7,7 @@ module Language.GraphQL.Error
, addErrMsg
, runCollectErrs
, runAppendErrs
, singleError
) where
import qualified Data.Aeson as Aeson
@ -46,12 +47,19 @@ type CollectErrsT m = StateT [Aeson.Value] m
addErr :: Monad m => Aeson.Value -> CollectErrsT m ()
addErr v = modify (v :)
makeErrorMsg :: Text -> Aeson.Value
makeErrorMsg s = Aeson.object [("message", Aeson.toJSON s)]
makeErrorMessage :: Text -> Aeson.Value
makeErrorMessage s = Aeson.object [("message", Aeson.toJSON s)]
-- | Constructs a response object containing only the error with the given
-- message.
singleError :: Text -> Aeson.Value
singleError message = Aeson.object
[ ("errors", Aeson.toJSON [makeErrorMessage message])
]
-- | Convenience function for just wrapping an error message.
addErrMsg :: Monad m => Text -> CollectErrsT m ()
addErrMsg = addErr . makeErrorMsg
addErrMsg = addErr . makeErrorMessage
-- | Appends the given list of errors to the current list of errors.
appendErrs :: Monad m => [Aeson.Value] -> CollectErrsT m ()

View File

@ -29,15 +29,11 @@ execute
execute schema subs doc =
maybe transformError (document schema) $ Transform.document subs doc
where
transformError = return $ Aeson.object
[("errors", Aeson.toJSON
[ Aeson.object [("message", "Schema transformation error.")]
]
)]
transformError = return $ singleError "Schema transformation error."
document :: MonadIO m => Schema m -> AST.Core.Document -> m Aeson.Value
document schema (op :| []) = operation schema op
document _ _ = error "Multiple operations not supported yet"
document _ _ = return $ singleError "Multiple operations not supported yet."
operation :: MonadIO m => Schema m -> AST.Core.Operation -> m Aeson.Value
operation schema (AST.Core.Query flds)