summaryrefslogtreecommitdiff
path: root/main.hs
diff options
context:
space:
mode:
authorBen Sima <ben@bsima.me>2018-01-22 22:15:24 -0800
committerBen Sima <ben@bsima.me>2018-01-22 22:15:24 -0800
commitf221c38f2c6c5f99d2e88821cba63cebc7a04221 (patch)
tree6614b1344b27cbf6caa67cc1613902a09ec44d74 /main.hs
parent273a70e84ba78e1638027be806e06e233d44e0c1 (diff)
Standardize errors
Diffstat (limited to 'main.hs')
-rwxr-xr-xmain.hs21
1 files changed, 17 insertions, 4 deletions
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) ]