introduce InvalidBody as the 'stickiest' route mismatch reason. code review changes too, to close #14 close #11 close #10

This commit is contained in:
Alp Mestanogullari 2014-10-28 16:50:42 +01:00
parent 812922d9b6
commit 9ade9a43f3
2 changed files with 16 additions and 9 deletions

View File

@ -16,11 +16,9 @@ instance (HasServer a, HasServer b) => HasServer (a :<|> b) where
type Server (a :<|> b) = Server a :<|> Server b type Server (a :<|> b) = Server a :<|> Server b
route Proxy (a :<|> b) request respond = route Proxy (a :<|> b) request respond =
route pa a request $ \ mResponse -> route pa a request $ \ mResponse ->
case isMismatch mResponse of if isMismatch mResponse
True -> route pb b request $ \mResponse' -> then route pb b request $ \mResponse' -> respond (mResponse <> mResponse')
respond (mResponse <> mResponse') else respond mResponse
False -> respond mResponse
where pa = Proxy :: Proxy a where pa = Proxy :: Proxy a
pb = Proxy :: Proxy b pb = Proxy :: Proxy b

View File

@ -27,6 +27,8 @@ toApplication ra request respond = do
respond $ responseLBS notFound404 [] "not found" respond $ responseLBS notFound404 [] "not found"
routingRespond (Left WrongMethod) = routingRespond (Left WrongMethod) =
respond $ responseLBS methodNotAllowed405 [] "method not allowed" respond $ responseLBS methodNotAllowed405 [] "method not allowed"
routingRespond (Left InvalidBody) =
respond $ responseLBS badRequest400 [] "Invalid JSON in request body"
routingRespond (Right response) = routingRespond (Right response) =
respond response respond response
@ -34,18 +36,25 @@ toApplication ra request respond = do
data RouteMismatch = data RouteMismatch =
NotFound -- ^ the usual "not found" error NotFound -- ^ the usual "not found" error
| WrongMethod -- ^ a more informative "you just got the HTTP method wrong" error | WrongMethod -- ^ a more informative "you just got the HTTP method wrong" error
| InvalidBody -- ^ an even more informative "you json request body wasn't valid" error
deriving (Eq, Show) deriving (Eq, Show)
-- | -- |
-- @ -- @
-- 'NotFound' <> x = x -- > mempty = NotFound
-- 'WrongMethod' <> _ = 'WrongMethod' -- >
-- > NotFound `mappend` x = x
-- > WrongMethod `mappend` InvalidBody = InvalidBody
-- > WrongMethod `mappend` _ = WrongMethod
-- > InvalidBody `mappend` _ = InvalidBody
-- @ -- @
instance Monoid RouteMismatch where instance Monoid RouteMismatch where
mempty = NotFound mempty = NotFound
NotFound `mappend` x = x NotFound `mappend` x = x
WrongMethod `mappend` _ = WrongMethod WrongMethod `mappend` InvalidBody = InvalidBody
WrongMethod `mappend` _ = WrongMethod
InvalidBody `mappend` _ = InvalidBody
-- | A wrapper around @'Either' 'RouteMismatch' a@. -- | A wrapper around @'Either' 'RouteMismatch' a@.
newtype RouteResult a = newtype RouteResult a =