summaryrefslogtreecommitdiff
path: root/Data/GraphQL/Error.hs
blob: 8c24a813ebf2eda92f5e914fde0720363dcd2b21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
{-# LANGUAGE OverloadedStrings #-}
module Data.GraphQL.Error (
  parseError,
  CollectErrsT,
  addErr,
  addErrMsg,
  runCollectErrs,
  joinErrs,
  errWrap
  ) where

import qualified Data.Aeson as Aeson
import Data.Text (Text, pack)

import Control.Arrow ((&&&))

-- | Wraps a parse error into a list of errors.
parseError :: Applicative f => String -> f Aeson.Value
parseError s =
  pure $ Aeson.object [("errors", Aeson.toJSON [makeErrorMsg $ pack s])]

-- | A wrapper for an 'Applicative' to pass error messages around.
type CollectErrsT f a = f (a,[Aeson.Value])

-- | Takes a (wrapped) list (foldable functor) of values and errors,
--   joins the values into a list and concatenates the errors.
joinErrs
  :: (Functor m, Functor f, Foldable f)
  => m (f (a,[Aeson.Value])) -> CollectErrsT m (f a)
joinErrs = fmap $ fmap fst &&& concatMap snd

-- | Wraps the given 'Applicative' to handle errors
errWrap :: Functor f => f a -> f (a, [Aeson.Value])
errWrap = fmap (flip (,) [])

-- | Adds an error to the list of errors.
addErr :: Functor f => Aeson.Value -> CollectErrsT f a -> CollectErrsT f a
addErr v = (fmap . fmap) (v :)

makeErrorMsg :: Text -> Aeson.Value
makeErrorMsg s = Aeson.object [("message",Aeson.toJSON s)]

-- | Convenience function for just wrapping an error message.
addErrMsg :: Functor f => Text -> CollectErrsT f a -> CollectErrsT f a
addErrMsg = addErr . makeErrorMsg

-- | Runs the given query, but collects the errors into an error
--   list which is then sent back with the data.
runCollectErrs :: Functor f => CollectErrsT f Aeson.Value -> f Aeson.Value
runCollectErrs = fmap finalD
  where
    finalD (dat,errs) =
        Aeson.object
      $ if null errs
           then [("data",dat)]
           else [("data",dat),("errors",Aeson.toJSON $ reverse errs)]