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 ()