question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Weird EntityResponseGenerator behaviour since 0.20.0-M7

See original GitHub issue
package repro

import cats.implicits._
import cats.effect._
import org.http4s.implicits._
import org.http4s.HttpRoutes
import org.http4s.server.blaze.BlazeServerBuilder

object App extends IOApp {
  override def run(args: List[String]): IO[ExitCode] =
    server[IO].compile.drain.as(ExitCode.Success)


  def server[F[_]: ConcurrentEffect: Timer] =
    BlazeServerBuilder[F]
      .bindHttp(8080, "0.0.0.0")
      .withHttpApp(routes[F].orNotFound)
      .serve

  def routes[F[_]: Sync]: HttpRoutes[F] = {
    val dsl = org.http4s.dsl.Http4sDsl[F]
    import dsl._
    HttpRoutes.of {
      // this results in empty response, used to result in 500 
      case _ => Ok(prg)
    }

    // this works as expected, returns 500
//    HttpRoutes.of {
//      case _ => for {
//        x <- prg
//        resp <- Ok(x)
//      } yield resp
//    }
  }

  def prg[F[_]: Sync]: F[String] = Sync[F].raiseError(new RuntimeException("oh no"))

}

I think the cause is that overload of EntityResponseGenerator.apply used to just mean flatMap (up to 0.20.0-M6), but now it’s deferring evaluating the F[A]-program in a Stream.eval of the body.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
SystemFwcommented, Jul 19, 2019

basically here https://github.com/http4s/http4s/blob/b3ea4b22fee858310ab88e9d5186457f3ec4482a/dsl/src/main/scala/org/http4s/dsl/impl/ResponseGenerator.scala#L50

you have this

 def apply[A](body: G[A])(implicit F: Applicative[F], w: EntityEncoder[G, A]): F[Response[G]] = {
    val entity = Entity(fs2.Stream.eval(body).flatMap(w.toEntity(_).body))
    val headers = addEntityLength(entity, w.headers)
    F.pure(Response[G](status = status, headers = headers, body = entity.body))
  }

F.pure(eval(body).flatMap(_.toEntity.body)) seems weird compared to body.map(_.toEntity.body) is there a reason for that that I don’t know?

(there is also a broader discussion about having such overloads, not a massive fan)

EDIT: Ah, it’s related to the introduction of the new type parameter here ofc 00cd51e ,that flatMap won’t compile. Interesting tradeoff since the reported behaviour is certainly a bit weird.

0reactions
rossabakercommented, Apr 15, 2021

Alright, I’ll leave it to you to decide if it works just as well there.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Search Results - CVE
The mission of the CVE® Program is to identify, define, and catalog publicly disclosed cybersecurity vulnerabilities.
Read more >
An Assessment of Industrial Energy Options Based on Coal ...
the emphasis of the study was on the Gulf Coast area, since industries in this region arc large energy consumers and the primary...
Read more >
0001193125-19-206598.txt - SEC.gov
The bases for calculating the returns were adjusted for the proposed amount of dividends after closure of the balance sheet still pending approval...
Read more >
Computer Systems
This book (known as CS:APP) is for computer scientists, computer engineers, and others who want to be able to write better programs by...
Read more >
license renewal environmental report additional information
For wastes not preprinted below: If you are a Very Small Quantity Generator, add the wastes to the list after the last preprinted....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found