@@ -111,11 +111,11 @@ corresponding API type.
111111The first thing to know about the `Server` type family is that behind the
112112scenes it will drive the routing, letting you focus only on the business
113113logic. The second thing to know is that for each endpoint, your handlers will
114−by default run in the `ExceptT ServantErr IO` monad. This is overridable very
114+by default run in the `Handler` monad. This is overridable very
115115easily, as explained near the end of this guide. Third thing, the type of the
116116value returned in that monad must be the same as the second argument of the
117117HTTP method combinator used for the corresponding endpoint. In our case, it
118−means we must provide a handler of type `ExceptT ServantErr IO [User]`. Well,
118+means we must provide a handler of type `Handler [User]`. Well,
119119we have a monad, let's just `return` our list:
120120
121121``` haskell
@@ -269,15 +269,15 @@ server3 = position
269269 :<|> hello
270270 :<|> marketing
271271
272− where position :: Int -> Int -> ExceptT ServantErr IO Position
272+ where position :: Int -> Int -> Handler Position
273273 position x y = return (Position x y)
274274
275− hello :: Maybe String -> ExceptT ServantErr IO HelloMessage
275+ hello :: Maybe String -> Handler HelloMessage
276276 hello mname = return . HelloMessage $ case mname of
277277 Nothing -> "Hello, anonymous coward"
278278 Just n -> "Hello, " ++ n
279279
280− marketing :: ClientInfo -> ExceptT ServantErr IO Email
280+ marketing :: ClientInfo -> Handler Email
281281 marketing clientinfo = return (emailForClient clientinfo)
282282```
283283
@@ -307,7 +307,7 @@ $ curl -X POST -d '{"clientName":"Alp Mestanogullari", "clientEmail" : "alp@foo.
307307
308308For reference, here's a list of some combinators from **servant**:
309309
310− > - `Delete`, `Get`, `Patch`, `Post`, `Put`: these do not become arguments. They provide the return type of handlers, which usually is `ExceptT ServantErr IO <something>`.
310+ > - `Delete`, `Get`, `Patch`, `Post`, `Put`: these do not become arguments. They provide the return type of handlers, which usually is `Handler <something>`.
311311 > - `Capture "something" a` becomes an argument of type `a`.
312312 > - `QueryParam "something" a`, `Header "something" a` all become arguments of type `Maybe a`, because there might be no value at all specified by the client for these.
313313 > - `QueryFlag "something"` gets turned into an argument of type `Bool`.
@@ -601,11 +601,10 @@ $ curl -H 'Accept: text/html' http://localhost:8081/persons
601601# or just point your browser to http://localhost:8081/persons
602602```
603603
604−## The `ExceptT ServantErr IO` monad
604+## The `Handler` monad
605605
606−At the heart of the handlers is the monad they run in, namely `ExceptT
607−ServantErr IO`
608−([haddock documentation for `ExceptT`](http://hackage.haskell.org/package/mtl-2.2.1/docs/Control-Monad-Except.html#t:ExceptT)).
606+At the heart of the handlers is the monad they run in, namely `ExceptT ServantErr IO`
607+([haddock documentation for `ExceptT`](http://hackage.haskell.org/package/mtl-2.2.1/docs/Control-Monad-Except.html#t:ExceptT)), which is aliased as `Handler`.
609608One might wonder: why this monad? The answer is that it is the
610609simplest monad with the following properties:
611610
@@ -621,7 +620,7 @@ Let's recall some definitions.
621620newtype ExceptT e m a = ExceptT (m (Either e a))
622621```
623622
624−In short, this means that a handler of type `ExceptT ServantErr IO a` is simply
623+In short, this means that a handler of type `Handler a` is simply
625624equivalent to a computation of type `IO (Either ServantErr a)`, that is, an IO
626625action that either returns an error or a result.
627626
@@ -688,7 +687,7 @@ module. If you want to use these values but add a body or some headers, just
688687use record update syntax:
689688
690689``` haskell
691−failingHandler :: ExceptT ServantErr IO ()
690+failingHandler :: Handler ()
692691failingHandler = throwError myerr
693692
694693 where myerr :: ServantErr
@@ -826,11 +825,11 @@ However, you have to be aware that this has an effect on the type of the
826825corresponding `Server`:
827826
828827``` haskell ignore
829−Server UserAPI3 = (Int -> ExceptT ServantErr IO User)
830− :<|> (Int -> ExceptT ServantErr IO ())
828+Server UserAPI3 = (Int -> Handler User)
829+ :<|> (Int -> Handler ())
831830
832−Server UserAPI4 = Int -> ( ExceptT ServantErr IO User
833− :<|> ExceptT ServantErr IO ()
831+Server UserAPI4 = Int -> ( Handler User
832+ :<|> Handler ()
834833 )
835834```
836835
@@ -842,10 +841,10 @@ computations in `ExceptT`, with no arguments. In other words:
842841server8 :: Server UserAPI3
843842server8 = getUser :<|> deleteUser
844843
845− where getUser :: Int -> ExceptT ServantErr IO User
844+ where getUser :: Int -> Handler User
846845 getUser _userid = error "..."
847846
848− deleteUser :: Int -> ExceptT ServantErr IO ()
847+ deleteUser :: Int -> Handler ()
849848 deleteUser _userid = error "..."
850849
851850-- notice how getUser and deleteUser
@@ -854,10 +853,10 @@ server8 = getUser :<|> deleteUser
854853server9 :: Server UserAPI4
855854server9 userid = getUser userid :<|> deleteUser userid
856855
857− where getUser :: Int -> ExceptT ServantErr IO User
856+ where getUser :: Int -> Handler User
858857 getUser = error "..."
859858
860− deleteUser :: Int -> ExceptT ServantErr IO ()
859+ deleteUser :: Int -> Handler ()
861860 deleteUser = error "..."
862861```
863862
@@ -905,23 +904,23 @@ type UsersAPI =
905904usersServer :: Server UsersAPI
906905usersServer = getUsers :<|> newUser :<|> userOperations
907906
908− where getUsers :: ExceptT ServantErr IO [User]
907+ where getUsers :: Handler [User]
909908 getUsers = error "..."
910909
911− newUser :: User -> ExceptT ServantErr IO ()
910+ newUser :: User -> Handler ()
912911 newUser = error "..."
913912
914913 userOperations userid =
915914 viewUser userid :<|> updateUser userid :<|> deleteUser userid
916915
917916 where
918− viewUser :: Int -> ExceptT ServantErr IO User
917+ viewUser :: Int -> Handler User
919918 viewUser = error "..."
920919
921− updateUser :: Int -> User -> ExceptT ServantErr IO ()
920+ updateUser :: Int -> User -> Handler ()
922921 updateUser = error "..."
923922
924− deleteUser :: Int -> ExceptT ServantErr IO ()
923+ deleteUser :: Int -> Handler ()
925924 deleteUser = error "..."
926925```
927926
@@ -940,23 +939,23 @@ data Product = Product { productId :: Int }
940939productsServer :: Server ProductsAPI
941940productsServer = getProducts :<|> newProduct :<|> productOperations
942941
943− where getProducts :: ExceptT ServantErr IO [Product]
942+ where getProducts :: Handler [Product]
944943 getProducts = error "..."
945944
946− newProduct :: Product -> ExceptT ServantErr IO ()
945+ newProduct :: Product -> Handler ()
947946 newProduct = error "..."
948947
949948 productOperations productid =
950949 viewProduct productid :<|> updateProduct productid :<|> deleteProduct productid
951950
952951 where
953− viewProduct :: Int -> ExceptT ServantErr IO Product
952+ viewProduct :: Int -> Handler Product
954953 viewProduct = error "..."
955954
956− updateProduct :: Int -> Product -> ExceptT ServantErr IO ()
955+ updateProduct :: Int -> Product -> Handler ()
957956 updateProduct = error "..."
958957
959− deleteProduct :: Int -> ExceptT ServantErr IO ()
958+ deleteProduct :: Int -> Handler ()
960959 deleteProduct = error "..."
961960```
962961
@@ -985,11 +984,11 @@ type APIFor a i =
985984
986985-- Build the appropriate 'Server'
987986-- given the handlers of the right type.
988−serverFor :: ExceptT ServantErr IO [a] -- handler for listing of 'a's
989− -> (a -> ExceptT ServantErr IO ()) -- handler for adding an 'a'
990− -> (i -> ExceptT ServantErr IO a) -- handler for viewing an 'a' given its identifier of type 'i'
991− -> (i -> a -> ExceptT ServantErr IO ()) -- updating an 'a' with given id
992− -> (i -> ExceptT ServantErr IO ()) -- deleting an 'a' given its id
987+serverFor :: Handler [a] -- handler for listing of 'a's
988+ -> (a -> Handler ()) -- handler for adding an 'a'
989+ -> (i -> Handler a) -- handler for viewing an 'a' given its identifier of type 'i'
990+ -> (i -> a -> Handler ()) -- updating an 'a' with given id
991+ -> (i -> Handler ()) -- deleting an 'a' given its id
993992 -> Server (APIFor a i)
994993serverFor = error "..."
995994-- implementation left as an exercise. contact us on IRC
@@ -998,12 +997,11 @@ serverFor = error "..."
998997
999998## Using another monad for your handlers
1000999
1001−Remember how `Server` turns combinators for HTTP methods into `ExceptT
1002−ServantErr IO`? Well, actually, there's more to that. `Server` is actually a
1000+Remember how `Server` turns combinators for HTTP methods into `Handler`? Well, actually, there's more to that. `Server` is actually a
10031001simple type synonym.
10041002
10051003``` haskell ignore
1006−type Server api = ServerT api (ExceptT ServantErr IO)
1004+type Server api = ServerT api Handler
10071005```
10081006
10091007`ServerT` is the actual type family that computes the required types for the
@@ -1036,12 +1034,11 @@ listToMaybeNat = Nat listToMaybe -- from Data.Maybe
10361034
10371035(`Nat` comes from "natural transformation", in case you're wondering.)
10381036
1039−So if you want to write handlers using another monad/type than `ExceptT
1040−ServantErr IO`, say the `Reader String` monad, the first thing you have to
1037+So if you want to write handlers using another monad/type than `Handler`, say the `Reader String` monad, the first thing you have to
10411038prepare is a function:
10421039
10431040``` haskell ignore
1044−readerToHandler :: Reader String :~> ExceptT ServantErr IO
1041+readerToHandler :: Reader String :~> Handler
10451042```
10461043
10471044Let's start with `readerToHandler'`. We obviously have to run the `Reader`
@@ -1050,10 +1047,10 @@ from that and can then just `return` it into `ExceptT`. We can then just wrap
10501047that function with the `Nat` constructor to make it have the fancier type.
10511048
10521049``` haskell
1053−readerToHandler' :: forall a. Reader String a -> ExceptT ServantErr IO a
1050+readerToHandler' :: forall a. Reader String a -> Handler a
10541051readerToHandler' r = return (runReader r "hi")
10551052
1056−readerToHandler :: Reader String :~> ExceptT ServantErr IO
1053+readerToHandler :: Reader String :~> Handler
10571054readerToHandler = Nat readerToHandler'
10581055```
10591056
@@ -1077,8 +1074,7 @@ readerServerT = a :<|> b
10771074```
10781075
10791076We unfortunately can't use `readerServerT` as an argument of `serve`, because
1080−`serve` wants a `Server ReaderAPI`, i.e., with handlers running in `ExceptT
1081−ServantErr IO`. But there's a simple solution to this.
1077+`serve` wants a `Server ReaderAPI`, i.e., with handlers running in `Handler`. But there's a simple solution to this.
10821078
10831079### Enter `enter`
10841080