liveBook · Manning

ExceptT

As explained in Functional Design and Architecture: Examples in Haskell

ExceptT is a Haskell monad transformer that adds computations which can terminate with an error value to an underlying monad.

ExceptT adds computations that may terminate with an error value to an underlying monad. In a Servant handler, it converts typed failures into ServerError values while leaving IO available for business-logic effects. section 14.2.1

type AstroAPI
  = (  "meteor"                         #1
    :> ReqBody '[JSON] MeteorTemplate   #2
    :> Post '[JSON] MeteorId            #3
    )
  :<|>
    (  «asteroid»                       #4
    :> ReqBody '[JSON] AsteroidTemplate #5
    :> Post '[JSON] AsteroidId          #6
    )

The Handler alias makes the transformer’s role explicit: handlers return either a Servant error or a successful result while performing IO. section 14.2.1

A database-backed handler

type Handler a = ExceptT ServerError IO a getObject :: AstroObjectId -> Handler (Maybe AstroObject) getObject objectId = do dbConn <- liftIO (DB.connect dbConfig) liftIO $ DB.query dbConn ("SELECT * FROM astro_objects WHERE id == " ++ show objectId) The handler uses liftIO for database effects while retaining Handler as its result context. section 14.2.1

ExceptT also forms the error layer inside a larger server stack: ReaderT supplies runtime configuration, ExceptT handles Servant exceptions, and IO performs network calls. hoistServer then converts the custom stack into Servant’s native environment. section 14.2.3 section 14.2.3

How Functional Design and Architecture: Examples in Haskell covers it

How deep it goes
Defines the Handler alias and demonstrates lifting database effects, while deferring transformer internals.
Examples
Uses Handler, getObject, the AppHandler stack, and the hoistServer conversion function.
What you can do after
Apply it via Servant to define handlers that combine recoverable errors with real effects.
How it connects
Connects error handling with Servant, ServerError, ReaderT, IO, hoistServer, and WAI.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Design and Architecture: Examples in Haskell. The section links point to the parts of the book it draws on.