Skip to content

Commit

Permalink
Return a 404 when we can't find a matching user
Browse files Browse the repository at this point in the history
  • Loading branch information
bradparker committed Oct 2, 2019
1 parent 10aa133 commit 2521f34
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
1 change: 1 addition & 0 deletions how-does-servants-type-dsl-work.cabal
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ executable server
main-is: Main.hs main-is: Main.hs
build-depends: base ^>=4.12.0.0 build-depends: base ^>=4.12.0.0
, aeson , aeson
, mtl
, servant-server , servant-server
, time , time
, wai , wai
Expand Down
6 changes: 5 additions & 1 deletion src/Main.hs
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@


module Main where module Main where


import Control.Monad.Error.Class
( throwError
)
import Data.Aeson (ToJSON) import Data.Aeson (ToJSON)
import Data.Foldable (find) import Data.Foldable (find)
import Data.Proxy (Proxy(Proxy)) import Data.Proxy (Proxy(Proxy))
Expand All @@ -21,6 +24,7 @@ import Servant
, Handler , Handler
, JSON , JSON
, Server , Server
, err404
, serve , serve
) )


Expand Down Expand Up @@ -72,7 +76,7 @@ matchesUsername uname = (uname ==) . username
usersShow :: String -> Handler User usersShow :: String -> Handler User
usersShow uname = usersShow uname =
case find (matchesUsername uname) users of case find (matchesUsername uname) users of
Nothing -> _ Nothing -> throwError err404
Just user -> pure user Just user -> pure user


usersServer :: Server UsersAPI usersServer :: Server UsersAPI
Expand Down

0 comments on commit 2521f34

Please sign in to comment.