japgolly / scalajs-react

Facebook's React on Scala.JS
https://japgolly.github.io/scalajs-react/
Apache License 2.0
1.64k stars 231 forks source link

AsyncCallback + cats, gracefully handling errors with EitherT: unexpected behaviour #1093

Closed Angel-O closed 2 months ago

Angel-O commented 2 months ago

Hi, I'm trying to handle errors gracefully. Imagine the following scenario


import cats.data.EitherT
import japgolly.scalajs.react.{Callback, React}
import japgolly.scalajs.react.callback.AsyncCallback
import japgolly.scalajs.react.callback.CallbackCats._

case class Data(name: String, age: Int)

val httpRequest: AsyncCallback[Data] = ???

val handledGracefully: AsyncCallback[Unit] = 
  EitherT
  .liftF[F, Throwable, Data](httpRequest)
  .semiFlatTap(d => Callback.log(s"all good, data is $d").async)
  .leftSemiflatTap(err => Callback.log(s"ooops, ${err.getMessage}").async)
  .merge

I would expect the above to be handled gracefully, but I'm seeing errors being raised.

Notes

I'm also parsing the response with circe, so there are a few things that could go wrong as part of the request, but I would expect any error to be caught and handled gracefully, instead I'm seeing unhandled errors in the js console.

I was wondering if this (cats + callback) is the expected behaviour, since it is different from cats + cats effect,

Thank you

Angel-O commented 2 months ago

nvm, realised this isn't meant to be handled gracefully