Baking-App-Kotlin: Retrofit Coroutine Exception Handling

Does the current view state handle retrofit exception? E.g: 4xx or 5xx Retrofit error response/body

CMIIW, I cannot find try-catch blocks in your ViewModel, Processor, Remote & Repository to handle that

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 16 (7 by maintainers)

Most upvoted comments

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()

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

@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