Skip to content

Commit 2521f34

Browse files
committed
Return a 404 when we can't find a matching user
1 parent 10aa133 commit 2521f34

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

how-does-servants-type-dsl-work.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ executable server
1010
main-is: Main.hs
1111
build-depends: base ^>=4.12.0.0
1212
, aeson
13+
, mtl
1314
, servant-server
1415
, time
1516
, wai

src/Main.hs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
module Main where
88

9+
import Control.Monad.Error.Class
10+
( throwError
11+
)
912
import Data.Aeson (ToJSON)
1013
import Data.Foldable (find)
1114
import Data.Proxy (Proxy(Proxy))
@@ -21,6 +24,7 @@ import Servant
2124
, Handler
2225
, JSON
2326
, Server
27+
, err404
2428
, serve
2529
)
2630

@@ -72,7 +76,7 @@ matchesUsername uname = (uname ==) . username
7276
usersShow :: String -> Handler User
7377
usersShow uname =
7478
case find (matchesUsername uname) users of
75-
Nothing -> _
79+
Nothing -> throwError err404
7680
Just user -> pure user
7781

7882
usersServer :: Server UsersAPI

0 commit comments

Comments
 (0)