diff options
Diffstat (limited to 'main.hs')
-rwxr-xr-x | main.hs | 21 |
1 files changed, 17 insertions, 4 deletions
@@ -19,10 +19,11 @@ {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE ScopedTypeVariables #-} +import Data.Semigroup import GHC.Generics import Control.Monad import Data.Maybe (isJust) -import Data.Text (Text, unpack) +import Data.Text import Yesod import Network.HTTP.Types.Status (status400, status200) import Data.Aeson @@ -50,6 +51,13 @@ data Caller = Caller } deriving (Show, Eq, Generic, ToJSON, FromJSON) + +-- | Standardize errors. +data ApiError = ApiError + { msg :: Text + } + deriving (Show, Eq, Generic, ToJSON, FromJSON) + -- FIXME findByNumber = id @@ -57,7 +65,9 @@ getQueryR :: Handler RepJson getQueryR = do qm <- lookupGetParam "number" case qm of - Nothing -> notFound + Nothing -> + sendStatusJSON status400 $ (ApiError "Invalid request. Could not parse 'number' parameter.") + Just q -> sendStatusJSON status200 $ object [ "msg" .= ("FIXME" :: Text) ] @@ -66,5 +76,8 @@ postNumberR :: Handler RepJson postNumberR = do (obj :: Result Value) <- parseJsonBody case obj of - Error err -> sendStatusJSON status400 $ object [ "msg" .= ("invalid body" :: Text) ] - Success v -> sendStatusJSON status200 $ object [ "msg" .= ("FIXME" :: Text) ] + Error err -> + sendStatusJSON status400 $ ApiError $ "Invalid request. Could not parse JSON body: " <> pack err + + Success v -> + sendStatusJSON status200 $ object [ "msg" .= ("FIXME" :: Text) ] |