Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only Handlers with Request input can have HandlerAspects applied (#2527) #2600

Merged
merged 1 commit into from
Jan 7, 2024

Conversation

987Nabil
Copy link
Contributor

@987Nabil 987Nabil commented Jan 7, 2024

This fix gives Handler#@@ the meaning it was supposed to have. It should only work on handlers where In =:= Request.
But that was not true. Because of the way the check was implemented, scala could infer Nothing for the In1and it would compile. Leading then to the runtime error one can see in #2527.

The more interesting question is: Can we define a generic HandlerAspect.identity and a generic version of Handler#@@ that not only accepts this identity but also other similar aspects?
After some playing around, I think the answer is probably no.
The two versions of @@ that exist, both return a Handler that has a In =:= Request. But in my opinion, a true HandlerAspect.identity, that is not only defined for a Handler that has a In =:= Request, must preserve the In type.

No applyX version of HandlerAspect can do this currently. The one that would be a candidate to be changed in that way would be applyHandler.

So I tried

  def applyHandler[Env1 <: Env, In](handler: Handler[Env1, Response, In, Response]): Handler[Env1, Response, In, Response] =
    if (self == HandlerAspect.identity) handler
    else {
      for {
        tuple <- protocol.incomingHandler
        (state, (request, ctxOut)) = tuple
        in <- Handler.fromFunction[In]((in: In) => in)
        either   <- Handler.fromZIO(handler(in)).either
        response <- Handler.fromZIO(protocol.outgoingHandler((state, either.merge)))
        response <- if (either.isLeft) Handler.fail(response) else Handler.succeed(response)
      } yield response
    }

But becauseprotocol.incomingHandler requires Request, I endup with a result type of Handler[Env1, Response, In with Request, Response] instead of Handler[Env1, Response, In, Response].

@jdegoes @vigoo If you see a way how to solve this, please let me know and I'll adjust the PR.

fixes #2527
/claim #2527

@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (f4b2b3f) 64.23% compared to head (a0659f4) 64.24%.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2600   +/-   ##
=======================================
  Coverage   64.23%   64.24%           
=======================================
  Files         140      140           
  Lines        8358     8359    +1     
  Branches     1604     1512   -92     
=======================================
+ Hits         5369     5370    +1     
  Misses       2989     2989           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@jdegoes jdegoes merged commit d297871 into zio:main Jan 7, 2024
34 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Middleware for a specific handler with parameters causes runtime casting error
3 participants