3

I'm using Servant with custom monad stack:

newtype AppHandler a = AppHandler { runHandler :: ReaderT Config (ExceptT ServantErr IO) a }
  deriving (Functor, Applicative, Monad, MonadReader Config, MonadError ServantErr, MonadIO)

data Config = Config
    { getPool :: ConnectionPool }

Now, in many handlers I just need to do fetch some data (Persistent) from db and act upon it, so I've got:

runDb :: (MonadReader Config m, MonadIO m) => SqlPersistT IO b -> m b
runDb query = do
  pool <- asks getPool
  liftIO $ runSqlPool query pool

As it turns out, when fetching from db, you're bound to work with Maybe, and quite often when the Maybe is Nothing, you just want to throw error so that Servant server will turn it into proper HTTP response. This led me to the discovery of Control.Error.Util and the (!?) :: Applicative m => m (Maybe a) -> e -> ExceptT e m a helper. So I tried following:

someHandler :: AppHandler NoContent
someHandler = do
  entity <- (runDb $ getCompanyByName companyName) !? err400
  -- some more logic
  return NoContent

But this does not compile, the result of !? here is ExceptT ServantErr m0 (Entity SomeEntity) but I no longer work with such handler type, it requires the AppHandler (Entity SomeEntity). How would I convert such value back to my handler type?

Bartosz
  • 3,318
  • 21
  • 31

2 Answers2

3

You want a variant of (!?) that is polymorphic in which monad it returns. For example:

(!??) :: MonadError e m => m (Maybe a) -> e -> m a
act !?? err = act >>= maybe (throwError err) return

Then, provided err400 :: ServantError -- which is the type of errors you declared AppHandler to have -- you will be able to write

runDb (getCompanyByName companyName) !?? err400
Daniel Wagner
  • 145,880
  • 9
  • 220
  • 380
  • This works, however, I'm bit puzzled I've had to write my own helper for that, I expected I could use something existing and compose (probably with some lifting). Wouldn't it be possible without writing new helper? – Bartosz May 30 '16 at 09:02
  • 1
    @Bartosz, you can open an issue or file a pull request with Daniel's code here: https://github.com/Gabriel439/Haskell-Errors-Library/issues, and then everyone in the future won't have to write their own helper :) – Beerend Lauwers May 30 '16 at 09:06
1

In general to convert a value of type m a to ReaderT r m a just use lift.

So perhaps this will work for you:

entity <- lift $ (runDb $ getCompanyByName companyName) !? err400

if the entire (runDb ...) !? err400 is an ExceptT ServantError ... value.

Also, this servant issue discussion:

https://github.com/haskell-servant/servant/issues/286

might be helpful.

ErikR
  • 51,541
  • 9
  • 73
  • 124
  • Yes, lift was my first intuition, however, compiler couldn't match `t0 (ExceptT ServantErr m0)` with `AppHandler`. There is no MonadTrans instance for AppHandler and I had troubles deriving/making one, as the kind of the AppHandler didn't match what MonadTrans expected. – Bartosz May 30 '16 at 08:59
  • I think you can get away with just a `type` for `AppHandler` - e.g. `type AppHandler = ReaderT Config (ExceptT ServantErr IO)`. You still need to use `!??` though. – ErikR May 30 '16 at 14:35