zio / zio-http

A next-generation Scala framework for building scalable, correct, and efficient HTTP clients and servers
https://zio.dev/zio-http
Apache License 2.0
787 stars 396 forks source link

Accept Routes with arbitrary error type in `PathCodec#/` #2934

Closed achinaou closed 2 months ago

achinaou commented 3 months ago

Is your feature request related to a problem? Please describe.

Currently, PathCodec#/ requires the Routes passed to it to have the errors handled and/or mapped to Response.

This limits where it can be used, as someone may want to handle the errors at a later point.

//> using dep dev.zio::zio-http:3.0.0-RC8

import zio.*
import zio.http.*
import zio.http.codec.PathCodec.literal

object Application extends ZIOAppDefault:

  val routes: Routes[Any, NotImplementedError] =
    literal("welcome") / Routes(
      Method.GET / Root -> handler:
        ZIO.fail(NotImplementedError())
    )

  def run =
    Server
      .serve(routes.sandbox)
      .provide(Server.default)

This code fails to compile with the error:

[error] None of the overloaded alternatives of method / in trait PathCodec with types
[error]  [Env]
[error]   (routes: zio.http.Routes[Env, zio.http.Response])
[error]     (implicit ev: zio.http.codec.PathCodec[Unit] <:<
[error]       zio.http.codec.PathCodec[Unit]): zio.http.Routes[Env, zio.http.Response]
[error]  [B]
[error]   (that: zio.http.codec.PathCodec[B])
[error]     (implicit combiner: zio.http.codec.Combiner[Unit, B]):
[error]       zio.http.codec.PathCodec[combiner.Out]
[error] match arguments (zio.http.Routes[Any, NotImplementedError])
[error]     literal("welcome") / Routes(
[error]     ^^^^^^^^^^^^^^^^^^^^

Describe the solution you'd like

It would be preferable not to force the user to handle the errors at this point and have the above code compile successfully.

jdegoes commented 2 months ago

/bounty $75 for fix and test case.

algora-pbc[bot] commented 2 months ago

💎 $75 bounty • ZIO

Steps to solve:

  1. Start working: Comment /attempt #2934 with your implementation plan
  2. Submit work: Create a pull request including /claim #2934 in the PR body to claim the bounty
  3. Receive payment: 100% of the bounty is received 2-5 days post-reward. Make sure you are eligible for payouts

Thank you for contributing to zio/zio-http!

Add a bountyShare on socials

Attempt Started (GMT+0) Solution
🟢 @987Nabil #2986
algora-pbc[bot] commented 2 months ago

💡 @987Nabil submitted a pull request that claims the bounty. You can visit your bounty board to reward.

algora-pbc[bot] commented 2 months ago

🎉🎈 @987Nabil has been awarded $75! 🎈🎊