From f221c38f2c6c5f99d2e88821cba63cebc7a04221 Mon Sep 17 00:00:00 2001 From: Ben Sima Date: Mon, 22 Jan 2018 22:15:24 -0800 Subject: Standardize errors --- main.hs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'main.hs') diff --git a/main.hs b/main.hs index dc1d440..966ea96 100755 --- a/main.hs +++ b/main.hs @@ -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) ] -- cgit v1.2.3