retrofit: Throwing retrofit2.HttpException HTTP 500 Internal Server Error, although exception is handled

I am using coroutines to call the api, If the response code is 500 then the app is crashing even though I’m catching the exception. Below is the implementation:

interface OnboardingApi {
        @PUT("/verifyOtp")
        suspend fun verifyOtp(@Body verifyOtpRequest: VerifyOtpRequest): Response<VerifyOtpResponse?>
}

override suspend fun verifyOtp(verifyOtpPayload: VerifyOtpPayload): VerifyOtpResponse? {
        try {
            val response = onboardingApi.verifyOtp(
                VerifyOtpRequest(
                    getDeviceDetails(sharedPreferencesHelper),
                    verifyOtpPayload.mobile,
                    verifyOtpPayload.otp
                )
            )
            return if (response.isSuccessful) {
                response.body()
            } else {
                RetrofitUtils.getErrorBody<VerifyOtpResponse>(response.errorBody())
            }
        } catch (e: Exception) {
            e.printStackTrace()
            FirebaseCrashlytics.getInstance().recordException(e)
        }
        return null
    }

and here is the stacktrace of the crash:
Fatal Exception: retrofit2.HttpException: HTTP 500 Internal Server Error
       at retrofit2.KotlinExtensions$await$2$2.onResponse(KotlinExtensions.java:53)
       at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:161)
       at com.google.firebase.perf.network.InstrumentOkHttpEnqueueCallback.onResponse(InstrumentOkHttpEnqueueCallback.java:71)
       at okhttp3.RealCall$AsyncCall.execute(RealCall.java:203)
       at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
       at java.lang.Thread.run(Thread.java:764)```

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 24
  • Comments: 15

Most upvoted comments

I have the same issue, any updates on this?