Frank Mung No
Frank Mung No

Reputation: 590

handle kotlin coroutines withContext exceptions inside try-catch not work

I call network operations like this.

viewModelScope.launch {
try {
        val data = withContext(dispatcher.io()) { homeRepository.getData() }
    } catch (e: Throwable) {
        val error = globalErrorHandler.getMessageForUser(throwable)
                Timber.d("seriesResponse failed: $error")
    }
}

and the response model is

data class MainResponse<T>(
    val list: List<T> = emptyList(),
    val total: Int?
)

but it still throw the following error.

Fatal Exception: com.squareup.moshi.JsonDataException
Non-null value 'credits' was null at $.data[1].credits

What's wrong with my code? I think it should catch the error.

UPDATED This is my mistake, this line val error = globalErrorHandler.getMessageForUser(throwable) cause the error, I forgot to handle it.

Upvotes: 0

Views: 266

Answers (1)

Frank Mung No
Frank Mung No

Reputation: 590

This is my mistake, this line val error = globalErrorHandler.getMessageForUser(throwable) cause the error, I forgot to handle it.

Upvotes: 0

Related Questions