Home > Net >  How can I handle catch exception for different functions at a common class in Kotlin?
How can I handle catch exception for different functions at a common class in Kotlin?

Time:03-15

This is my RemoteDataSource class, where I'll write many api calls like this:

suspend fun getReviewData1() = getResult {
        try {
           apiService.getReviewData(getCustomerId())
        } catch (e: Exception) {
           handleException(e)
        }
    }

suspend fun getReviewData2() = getResult {
        try {
           apiService.getReviewData(getCustomerId())
        } catch (e: Exception) {
           handleException(e)
        }
    }

Now, you can see that for each fun, I need to wrap my code in try/catch block. Everything is working fine, I'm able to catch the exception also, but why to write so many try/catch for each function? Instead I need to do in one common class, so that I can simply call my function SOMETHING like this.

suspend fun getReviewData() = getResult {
     apiService.getReviewData(getCustomerId())
 }

You can write an answer and suggest me if you want to make change anything, for ex in getResult()


getResult() in another base class:

protected suspend fun <T> getResult(call: suspend () -> Response<T>?): Resource<T> {
        try {
            val response = call()
            if (response?.isSuccessful == true) {
                val body = response.body()
                if (body != null) return Resource.success(body, response.message(), response.code())
            }
            return error((response?.message() ?: context.getString(R.string.unable_to_reach_server)),
                (response?.code() ?: AppConstants.INetworkValues.DEFAULT_ERROR_CODE))
        } catch (e: Exception) {
            return error(e.message ?: e.toString(), (call()?.code() ?: AppConstants.INetworkValues.DEFAULT_ERROR_CODE))
        }
    }

    private fun <T> error(message: String, code: Int): Resource<T> {
        LogUtils.d(message)
        return Resource.error(null, message, code)
    }

handleException()

fun handleException(e: Exception): Response<Any> {
        if (e is NoConnectivityException) {
            return Response.error(AppConstants.INetworkValues.INTERNET_ERROR_CODE, getDummyResponseBody())
        } else {
            return Response.error(AppConstants.INetworkValues.DEFAULT_ERROR_CODE, getDummyResponseBody())
        }
    }

I tried this answer, but it's just not happening: https://stackoverflow.com/a/46517243/14016240

Please help.

CodePudding user response:

Your getResult function has an error in the catch block. You are calling call() again if the first call to call() threw an exception. I don't think it makes sense to call it again, because it will probably throw again and you won't be able to return your Resource.error.

If you fix this problem, you won't need to use try/catch in your other functions, because call will be safely wrapped in try/catch and pack any errors into a Resource.error.

To fix it: since you already handle response?.code in the try block, there's no reason to repeat getting call()?.code in the catch block. You can simplify it to

return error(e.message ?: e.toString(), AppConstants.INetworkValues.DEFAULT_ERROR_CODE)
  • Related