https://kotlinlang.org logo
#arrow
Title
# arrow
j

jrgonzalez

07/12/2022, 4:33 PM
Hi, I’m trying to migrate some (network) Android code from Either to Effect and I’m getting weird crashes. Are the effect / Effect apis stable? Is there any known incompatibility with ktor? I have the happy path working now for some code, but whenever I try to do a shift my app crashes almost everytime. Debugger does not even stop on try-catches / folds, anything, it totally explodes bypassing all error handling. Stacktraces are like this:
r

raulraja

07/12/2022, 4:55 PM
Hi @jrgonzalez, Is this something you can reproduce on a small example? or can you show the code that produces the stack trace?
j

jrgonzalez

07/12/2022, 4:56 PM
sure, I have been trying different changes, the version I have right now is like this:
Copy code
suspend inline fun <reified R, reified A> get(
    url: Url,
    acceptedErrorCodes: List<Int> = emptyList()
  ): Effect<BackendFailure<R>, A> =
    effect {
      try {
        val httpResponse: HttpResponse = getHttpResponse(url)

        val httpStatusCode: HttpStatusCode = httpResponse.status

        if (httpStatusCode.isSuccess() || httpStatusCode.value in acceptedErrorCodes) {
          when (val backendResponse: BackendResponse<R, A> = httpResponse.body()) {
            is BackendResponse.Failure -> shift(BackendFailure.Known(backendResponse.data))

            is BackendResponse.Success -> backendResponse.data
          }
        } else {
          shift(BackendFailure.Unknown(null))
        }
      } catch (throwable: Throwable) {
        shift(BackendFailure.Unknown(throwable.nonFatalOrThrow().message))
      }
    }
it works if it does not go through any of the
shift
. As soon as it goes through them it produces a stacktrace like the one I gave most of the times
Adding a
shift<A>(BackendFailure.Unknown(null))
as first line on the
try
block sometimes works though, but other times it fails too which is odd cause it should not have reached any ktor-dependent code by then (first ktor call is on the
getHttpResponse
which is a simple ktor’s
httpClient.get(url)
)
r

raulraja

07/12/2022, 5:31 PM
What version of Arrow are you using? We addressed recently an issue that seems to be related to Kotlin's inline system here https://github.com/arrow-kt/arrow/pull/2734
Would be good to try with one of the latest published alphas after that PR got merged to see if the error goes away and if it's related.
j

jrgonzalez

07/12/2022, 5:32 PM
Mostly tried 1.1.2, but I also tried the latest 1.1.3 alpha and got the same issue. But I did not do a clean build, I will try again
going to try on arrow-stack 1.1.3-alpha.29
r

raulraja

07/12/2022, 5:34 PM
Although in your stack trace seems like what's bubbling up is an actual ShiftException, so it's most likely a bug on our side or the way the Ktor integration there is wired
j

jrgonzalez

07/12/2022, 5:36 PM
yes, I think it may have something to do with both Arrow and ktor tapping into the coroutine system with custom handling
but I have no idea what exactly or how to workaround it
r

raulraja

07/12/2022, 5:40 PM
I'm not familiar with Ktor but maybe there is something here from @simon.vergauwen’s sample repo or he knows what's going on. https://github.com/nomisRev/ktor-arrow-example
j

jrgonzalez

07/12/2022, 5:50 PM
yes, saw that, but it is mostly server-side ktor, while I’m trying client part
it does still happen on 1.1.3-alpha.29 even on clean build
r

raulraja

07/12/2022, 5:54 PM
Would you mind creating an issue in the repo and posting the link here? We can triage it in our next meeting and see what we can do about it. Thanks!
j

jrgonzalez

07/12/2022, 6:08 PM
Thanks!
7 Views