aboutsummaryrefslogtreecommitdiff
path: root/server/src/Controller/Helper.hs
blob: fd0d2bb4312ac5bf8c6552f7c9de91ca7c45faec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
module Controller.Helper
  ( jsonOrBadRequest
  ) where

import           Data.Aeson                (ToJSON)
import           Data.Text                 (Text)
import qualified Data.Text.Lazy            as LT
import qualified Network.HTTP.Types.Status as Status
import           Web.Scotty                (ActionM)
import qualified Web.Scotty                as S

jsonOrBadRequest :: forall a. (ToJSON a) => Either Text a -> ActionM ()
jsonOrBadRequest (Left message) = do
  S.status Status.badRequest400
  S.text (LT.fromStrict message)
jsonOrBadRequest (Right a) =
  S.json a