https://kotlinlang.org logo
Title
s

sitepodmatt

02/28/2019, 10:20 AM
class RequestContextTest : StringSpec({
    "playing with human context" {

        runBlocking {

            supervisorScope {


                try {
                    val repoSize = async<String> {
                        // do remote
                        val somenestedOp = async<String> {
                            delay(100)
                            throw DomainException("oh no", Exception("400"))
                        }
                        somenestedOp.await()
                    }

                    repoSize.await()
                } catch (de: DomainException) {
                    println("we will swallow")

                }


            }


        }
        println("we exited okay")
    }


})

class DomainException(msg: String, t: Throwable) : Exception(msg, t)
l

leosan

02/28/2019, 10:22 AM
Let’s say on this block I have to catch the exception and then throw the domain, would be fine just to catch, map and rethrow?
val somenestedOp = async<String> {
                            delay(100)
                            throw DomainException("oh no", Exception("400"))
                        }
s

sitepodmatt

02/28/2019, 10:25 AM
can you mock up an example? but if i think you're asking what you are then yes
l

leosan

02/28/2019, 10:29 AM
using your exemple, something on this line
val somenestedOp = async<String> {
    try{
        someOperation()
    }catch (de: DataException){
        val  mappedException = when(de){
            is HttpException -> throw DomainException("oh no", Exception("400"))
            is IOException -> throw OtherDomainException("oh no", Exception("300"))
        }
        throw mappedException()
    }
}
s

sitepodmatt

02/28/2019, 10:32 AM
yes that should work (presuming someOperation is not a special coroutinebuilder). the supervisor scope limits the propagation of cancellation upstream, presuming you handle the exception
l

leosan

02/28/2019, 10:32 AM
Oh thanks then 😄
s

sitepodmatt

02/28/2019, 10:35 AM
you can see how supervisorScope changes the behavior by swapping it coroutineScope and back, i.e. the exception despite being handled on await() still propagates up the chain
l

leosan

02/28/2019, 10:39 AM
Yeah I just finished the test here using the
runCatching{}.recover{}
and it worked beautifully I was just wondering if it was a common thing rethrowing errors, I was used to Rx way of composing the upstream with transformers + onErrorResumeNext
s

sitepodmatt

02/28/2019, 10:40 AM
I'm not familiar with the Rx way so not sure, there is an #rx channel incase youve not seen that though
l

leosan

02/28/2019, 10:42 AM
No problem at all