Git Product home page Git Product logo

Comments (16)

mochadwi avatar mochadwi commented on June 16, 2024 2

Agreed!

Previously we're using flow builder and after a while searching on the internet, then we're using the extension to reduce boilerplate, as stated in Kotlin Flow code it has same purpose:

Screen Shot 2020-08-08 at 8 53 50 PM

Or using method references approach:

override fun doSomething(): Flow<Abc> = repository::doAbc.asFlow()

from baking-app-kotlin.

Ezike avatar Ezike commented on June 16, 2024 1

@mochadwi Yes it does handle exceptions. When using flow, you handle exceptions by using the catch operator which will catch exceptions that occur upstream. The catch operator doesn't handle exceptions for any operations added after it. For example, in the RecipeActionProcessor, you'll find this code:

private val refreshData: Flow<RecipeViewResult>
        get() = recipes.map { recipes ->
           ...
        }.onStart {
            emit(RefreshRecipesResult.Refreshing)
        }.catch { cause: Throwable ->
            emit(RefreshRecipesResult.Error(cause))
        }

Check out this article too for more context.
https://medium.com/@elizarov/exceptions-in-kotlin-flows-b59643c940fb

from baking-app-kotlin.

mochadwi avatar mochadwi commented on June 16, 2024 1

Closing this issue, because it already answered our usecase. Thanks a lot & much appreciate it once again @Ezike

from baking-app-kotlin.

mochadwi avatar mochadwi commented on June 16, 2024

Our backend usecase has a custom body response using http status code for 4xx & 5xx

E.g image:

Screen Shot 2020-08-08 at 8 16 24 PM

from baking-app-kotlin.

mochadwi avatar mochadwi commented on June 16, 2024

Ah, I see. So the error catching was built-in when using Flow.

After reading your code, the .catch lambda exist on *ActionProcessor and returned with data class Error(val cause: Throwable) that explains everything!

from baking-app-kotlin.

mochadwi avatar mochadwi commented on June 16, 2024

In result, our code is much cleaner now without try-catch blocks!

from baking-app-kotlin.

Ezike avatar Ezike commented on June 16, 2024

Ah, I see. So the error catching was built-in when using Flow.

After reading your code, the .catch lambda exist on *ActionProcessor combined with data class Error(val cause: Throwable) that explains everything!

yea exactly

from baking-app-kotlin.

mochadwi avatar mochadwi commented on June 16, 2024

Thanks and really appreciate it!

from baking-app-kotlin.

Ezike avatar Ezike commented on June 16, 2024

In result, our code is much cleaner now without try-catch blocks!

Yea with flow you can use the catch operator, but with regular suspending functions, you still need to use a try / catch

from baking-app-kotlin.

mochadwi avatar mochadwi commented on June 16, 2024

In result, our code is much cleaner now without try-catch blocks!

Yea with flow you can use the catch operator, but with regular suspending functions, you still need to use a try / catch

Ah, I see. It's better to return a Flow (when using catch operator) instead of using suspend modifiers?

from baking-app-kotlin.

mochadwi avatar mochadwi commented on June 16, 2024

I've read in elizarov medium article and article that compare rx & coroutine here: http://disq.us/p/259lped

And my PoV was, like this:

fun returnListUseFlow(): Flow<List<Abc>>>

suspend fun returnSinglePojo(): Abc

@Ezike after you mention about suspend modifiers does that means, we have to use like this as well:

fun returnSinglePojo(): Flow<Abc>

CMIIW

from baking-app-kotlin.

Ezike avatar Ezike commented on June 16, 2024

In result, our code is much cleaner now without try-catch blocks!

Yea with flow you can use the catch operator, but with regular suspending functions, you still need to use a try / catch

Ah, I see. It's better to return a Flow (when using catch operator) instead of using suspend modifiers?

You can only use the catch operator with flow.
I guess if you're using mvi architecture you do need to wrap your suspending retrofit calls with flow

from baking-app-kotlin.

Ezike avatar Ezike commented on June 16, 2024

I've read in elizarov medium article and article that compare rx & coroutine here: http://disq.us/p/259lped

And my PoV was, like this:

fun returnListUseFlow(): Flow<List<Abc>>>

suspend fun returnSinglePojo(): Abc

@Ezike after you mention about suspend modifiers does that means, we have to use like this as well:

fun returnSinglePojo(): Flow<Abc>

CMIIW

If you're not wrapping your network call in a flow, you need to use try catch to handle exceptions. There's also a runCatching function that returns a result type that could either be a success or failure if you don't want to write try/catch blocks

from baking-app-kotlin.

mochadwi avatar mochadwi commented on June 16, 2024

There's also a runCatching function that returns a result type that could either be a success or failure if you don't want to write try/catch blocks

Awesome, noted will using runCatching for this usecases.

I guess if you're using mvi architecture you do need to wrap your suspending retrofit calls with flow
Ah, nice. We're using this extensions:

// extensions
fun <R> suspendAsFlow(block: suspend () -> R): Flow<R> = block.asFlow()

// repository
suspend fun doAbc(): Abc

// usecase
override fun doSomething(): Flow<Abc> = suspendAsFlow { repository.doAbc() } 

Or does flow already has extensions for transforming from suspend into Flow? @Ezike

from baking-app-kotlin.

Ezike avatar Ezike commented on June 16, 2024

Ah, nice. We're using this extensions:

// extensions
fun <R> suspendAsFlow(block: suspend () -> R): Flow<R> = block.asFlow()

// repository
suspend fun doAbc(): Abc

// usecase
override fun doSomething(): Flow<Abc> = suspendAsFlow { repository.doAbc() } 

Or does flow already has extensions for transforming from suspend into Flow? @Ezike

This is nice. You can also just use the flow builder and call your suspending function inside...

override fun doSomething(): Flow<Abc> = flow { emit(repository.doAbc()) }

from baking-app-kotlin.

Ezike avatar Ezike commented on June 16, 2024

Closing this issue, because it already answered our usecase. Thanks a lot & much appreciate it once again @Ezike

You're welcome

from baking-app-kotlin.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.