Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Throwing retrofit2.HttpException HTTP 500 Internal Server Error, although exception is handled #3626

Open
naveenchandan opened this issue Aug 25, 2021 · 14 comments

Comments

@naveenchandan
Copy link

naveenchandan commented Aug 25, 2021

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)```
@stepanzalis
Copy link

I have the same issue, any updates on this?

@ptornhult
Copy link

Is this really a Retrofit2 problem or an internal FirebasePerf problem (I'm having the same issue)

@giaesp
Copy link

giaesp commented Dec 9, 2021

I have the same issue with no dependencies on Firebase. The issue is in okhttp: any status code != 200 throws an unhandled exception, causing a crash. Timeout also causes app crash.

@drissfoo
Copy link

drissfoo commented Apr 5, 2022

any update on this still having it ?

@bahacan19
Copy link

any update on this still having it ?

I personally searched in my codebase and then found a code like this in a okhttp3.Authenticator. (inside of its authenticate() method)

someCoroutineScope.launch {
            // a retrofit call gets 4XX
        }

but it needs to have exception handlers:

scope.launch(CoroutineExceptionHandler { _, ex -> logThisException(ex) }) {
            // a retrofit call gets 4XX
        }

this is how i fixed for now.

@flopshot
Copy link

any update on this still having it ?

I personally searched in my codebase and then found a code like this in a okhttp3.Authenticator. (inside of its authenticate() method)

someCoroutineScope.launch {
            // a retrofit call gets 4XX
        }

but it needs to have exception handlers:

scope.launch(CoroutineExceptionHandler { _, ex -> logThisException(ex) }) {
            // a retrofit call gets 4XX
        }

this is how i fixed for now.

If I use that exception handler, I lose the original exception and get

CoroutineCancelationException

I need to recover the original OkHttp exception, which has messages from the server

@softsan
Copy link

softsan commented Apr 6, 2023

Same issue here.. any one found any solution yet?

@JakeWharton
Copy link
Collaborator

Without a failing test case or minimally-reproducing sample project that demonstrates the problem it's impossible to know what's going on. I'll leave the issue open for 30 days in case someone wants to make one of those to aid us in debugging.

@MahdiRahmani80
Copy link

Hi, I have this issue too
some of my users can register but some not, It is odd,

UserRepository

class UserRepository @Inject constructor(
    private val shared: UserSharedPreferenceRepository,
    private val repository: UserApiServiceRepository
) {

    fun getUser(phone: String): Flow<User> {
        return repository.getUser(phone).map { it.toExternal() }
    }

    suspend fun addUser(user: User): Flow<Boolean> = flow {
        repository.addUser(user.toApiModel()).collect { isSuccess ->
            if (isSuccess) {
                delay(80)
                val userApi = repository.getUser(user.phone).first()
                val saveSharedPref = user.toSharedPref()
                saveSharedPref.isLogin = isSuccess
                saveSharedPref.id = userApi.id
                emit(isSuccess)
                shared.setUserData(saveSharedPref)
            }

        }
    }

}

UserApiService

class UserApiServiceRepository @Inject constructor(
    private val api: UserApiService
) : UserDataStore {

    private val TAG = "UserApiServiceRepository"
    override fun getUser(phone: String): Flow<UserApiModel> = flow {
        try {
            val data = api.getUser(phone)
            emit(data)
        } catch (e: Exception) {
            if (e is CancellationException) {
                Log.e(TAG, e.message.toString())
                Bugsnag.notify(e)
                throw e
            }
        }

    }

    override suspend fun addUser(user: UserApiModel): Flow<Boolean> = flow {
        var bool = false
        try {
            api.createUser(user)
        } catch (e: Exception) {
            Log.e(TAG, e.message.toString())
            Bugsnag.notify(e)
            bool = !bool
        } finally {
            emit(!bool)
        }
    }

    override suspend fun updateUser(user: User): Flow<Boolean> = flow {
        var boolean = false
        try {
            api.updateUser(user.id.toString(), user.toApiModel())
        } catch (e: Exception) {
            Log.e(TAG, e.message.toString())
            Bugsnag.notify(e)
            boolean = !boolean
        } finally {
            emit(!boolean)
        }
    }

}

I using Bugsnag for error handling
and my error is:

retrofit2.HttpException: HTTP 500 Internal Server Error
        at retrofit2.KotlinExtensions$await$2$2.onResponse(KotlinExtensions.kt:53)
        at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:161)
        at okhttp3.RealCall$AsyncCall.execute(RealCall.java:174)
        at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
        at java.lang.Thread.run(Thread.java:929)

And the app crashes, but I can not find out why!

@JakeWharton
Copy link
Collaborator

Please provide an executable, minimally-reproducing test case or sample that exhibits the problem. We have our own tests that demonstrate exceptions can be caught just fine. If I cannot very simply execute something that exhibits the behavior you are seeing I cannot be sure of the problem.

@MahdiRahmani80
Copy link

MahdiRahmani80 commented Jun 13, 2024

the problem is "I can not reproduce this"
I tried to cURL my end point but I could not succeed in generating 500 errors!
my endpoints

in Kotlin my UserApiService interface:

interface UserApiService {

    @GET("users.php")
    suspend fun getUser(@Query("phone") phone: String): UserApiModel

    @POST("users.php")
    suspend fun createUser(@Body user: UserApiModel)

    @PUT("users.php")
    suspend fun updateUser(@Query("id") id: String, @Body user: UserApiModel)
}

this app now is not in production mode and everything is a test mode.

EDIT: I think the Android version of revealing this error is 10.
for a better looking my project see this link
this is a side project!

@JakeWharton
Copy link
Collaborator

Retrofit is not responsible for your server sending HTTP error codes. This issue is about the purported inability to catch the exceptions. MockWebServer or httpbin can be used to reliably deliver different HTTP codes for testing.

@MahdiRahmani80
Copy link

MahdiRahmani80 commented Jun 14, 2024

Retrofit is not responsible for your server sending HTTP error codes.

Ok,
But recently understood another thing, with breakpoints,
if even the try is successful the program falls to the catch method.
image

I think the problem is emitting
because the addUser method works correctly. (not falling to catch)

EDIT:
I changed to this and its not falling to catch:

    override fun getUser(phone: String): Flow<UserApiModel?> = flow {
        var data: UserApiModel? = null
        try {
            data = api.getUser(phone)
        } catch (e: Exception) {
            if (e is CancellationException) {
                Log.e(TAG, e.message.toString())
                Bugsnag.notify(e)
                throw e
            }
        } finally {
            emit(data)
        }

    }

@JakeWharton
Copy link
Collaborator

Compilation of suspend functions is controlled by Kotlin. We have no control of the code that executes around your call into Retrofit.

If you believe the problem is Retrofit, please create a self-contained, minimally-reproducing sample that I can run which demonstrates the behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

10 participants