cdepillabout / servant-checked-exceptions

type-level errors for Servant APIs.
https://hackage.haskell.org/package/servant-checked-exceptions
BSD 3-Clause "New" or "Revised" License
73 stars 14 forks source link

NamedRoutes not implemented #40

Open immae opened 1 year ago

immae commented 1 year ago

I’m trying to use the servant-checked-exceptions package with NamedRoutes. But I want to add the "Throws" at the top of the api, for instance:

data ApiFoo mode = ApiFoo
  {
    get :: mode :- Get '[JSON] Foo
   , post :: mode :- "new" :> ReqBody '[JSON] Foo :> Post '[JSON] ()
  }

data ApiStore mode = ApiStore
  {
    fooStore :: mode :- "foo" :> NamedRoutes ApiFoo
  , barStore :: mode :- "bar" :> NamedRoutes ApiBar

  }

type API = Throws StoreError :> "store" :> NamedRoutes ApiStore

But Throws is not able to traverse NamedRoutes types. I tried to implement a HasServer (Throwing '[e] :> NamedRoutes api) instance, but I seem unable to do it correctly (I tried to find other packages that implement a similar instance too to use as example, in vain). I’d appreciate help for implementing that.

Thanks!

cdepillabout commented 1 year ago

I've never actually used NamedRoutes before.

Could you upload a branch with the (non-working) code you currently have?

immae commented 1 year ago

Thanks for your consideration. I took a bit of time to extract what I tried to do from local specificities. You’ll find the attempt here: https://github.com/immae/servant-checked-exceptions/tree/wip-named-routes

I added an "example" file /Movies.hs (which I use in the repl to play around), using the NamedRoutes example from Servant themselves (https://docs.servant.dev/en/inserting_doc_namedroutes/cookbook/namedRoutes/NamedRoutes.html ), and tried to "build" an api that can Throw from it and add the needed instances. Somehow I got a bit further than what I achieved in my own local work (I have a feeling that the AsEnveloppedServerT is kind of necessary), but I’m still not there.

Sorry that I cannot help more, It’s easy to get lost in all the Servant types.

Hope this sketch helps a bit!

cdepillabout commented 1 year ago

I took a look at this, but I don't think I'll have time to grok exactly how Generic is being used in the implementation of NamedRoutes, at least to the point where I can use it to implement support here in servant-checked-exceptions.

For anyone else interested, here is the servant-server code that deals with NamedRoutes:

https://hackage.haskell.org/package/servant-server-0.20/docs/src/Servant.Server.Internal.html#AsServerT

I'd be happy to merge a PR adding support for NamedRoutes here in servant-checked-exceptions if someone is able to figure everything out.

immae commented 1 year ago

Thank you for taking the time to look at it.

I didn’t make progress so far (further than what I published already in the branch)