haskell-servant / servant

Main repository for the servant libraries — DSL for describing, serving, querying, mocking, documenting web applications and more!
https://docs.servant.dev/
1.8k stars 407 forks source link

Export a function of type `IO (Either ServerError a) -> Handler a` from `Servant.Server` #1732

Closed danidiaz closed 2 months ago

danidiaz commented 2 months ago

Currently we have runHandler :: Handler a -> IO (Either ServerError a), and it would be good to have a function going in the other direction.

Of course, it's easy to lift an IO a to Handler a using liftIO but, unless I'm mistaken, going from IO (Either ServerError a) to a Handler requires a dependeny on transformers and importing the ExceptT constructor.

Instead of a special-purpose function, re-exporting the ExceptT constructor could be another solution.

ysangkok commented 2 months ago

Sounds fairly innocent, I'd review a PR for this. But transformers is such a popular package, I am not sure I fully understand why one would avoid it. Is it just because it's cumbersome for something you think is something Servant should have out of the box?

danidiaz commented 2 months ago

For small enough cases, I do like to use Servant without tranformers and mtl. I just stay in the IO monad and lift the result to Handler at the last minute. I guess you could call it "simple Servant" 😆

I've just realised that I already can write a function IO (Either ServerError a) -> Handler a without those dependencies, because throwError is re-exported by the Servant module. Still, I believe having an explicit inverse to runHandler could make sense.

tchoutri commented 2 months ago

@danidiaz Are you happy with the PR? If yes, I'll let you close this ticket. :)