chore: Handle http 401 exc from global exc handler
This commit is contained in:
parent
5358051034
commit
82f300fdd9
|
@ -4,6 +4,7 @@ import android.content.Context
|
|||
import android.content.Intent
|
||||
import android.util.Log
|
||||
import com.p_vacho.neat_calendar.activities.ApiUnreachableActivity
|
||||
import com.p_vacho.neat_calendar.activities.LoginActivity
|
||||
import com.p_vacho.neat_calendar.util.ExceptionSerializer
|
||||
import com.p_vacho.neat_calendar.util.SerializedException
|
||||
import retrofit2.HttpException
|
||||
|
@ -15,19 +16,35 @@ class GlobalExceptionHandler(
|
|||
private val defaultHandler = Thread.getDefaultUncaughtExceptionHandler()
|
||||
|
||||
override fun uncaughtException(t: Thread, e: Throwable) {
|
||||
if (e is HttpException && e.code() == 503) {
|
||||
if (e !is HttpException) {
|
||||
// Let the default handler manage other exceptions
|
||||
defaultHandler?.uncaughtException(t, e)
|
||||
return
|
||||
}
|
||||
|
||||
if (e.code() == 503 && e.message() == "API is unreachable") {
|
||||
val errorBody = e.response()!!.errorBody()!!.string()
|
||||
val exception = ExceptionSerializer.deserialize(errorBody)
|
||||
Log.e("GlobalExceptionHandler", "Deserialized HTTP 503 Exception")
|
||||
Log.e("GlobalExceptionHandler", "Exception Type: ${exception.type}")
|
||||
Log.e("GlobalExceptionHandler", "Message: ${exception.message}")
|
||||
Log.e("GlobalExceptionHandler", "Stacktrace:\n${exception.stacktrace.joinToString("\n")}")
|
||||
Log.e(
|
||||
"GlobalExceptionHandler",
|
||||
"Stacktrace:\n${exception.stacktrace.joinToString("\n")}"
|
||||
)
|
||||
|
||||
navigateToApiUnreachableActivity()
|
||||
} else {
|
||||
// Let the default handler manage other exceptions
|
||||
defaultHandler?.uncaughtException(t, e)
|
||||
return
|
||||
}
|
||||
|
||||
if (e.code() == 401 && e.message() == "Session expired") {
|
||||
Log.w("GlobalExceptionHandler", "Caught HTTP 401 Exception, redirecting to login")
|
||||
navigateToLoginActivity()
|
||||
return
|
||||
}
|
||||
|
||||
defaultHandler?.uncaughtException(t, e)
|
||||
return
|
||||
}
|
||||
|
||||
private fun navigateToApiUnreachableActivity() {
|
||||
|
@ -36,4 +53,11 @@ class GlobalExceptionHandler(
|
|||
}
|
||||
context.startActivity(intent)
|
||||
}
|
||||
|
||||
private fun navigateToLoginActivity() {
|
||||
val intent = Intent(context, LoginActivity::class.java).apply {
|
||||
flags = Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK
|
||||
}
|
||||
context.startActivity(intent)
|
||||
}
|
||||
}
|
|
@ -131,11 +131,12 @@ class AuthInterceptor(
|
|||
}
|
||||
|
||||
private fun handleSessionExpired(chain: Interceptor.Chain): Response {
|
||||
Log.e("API", "Session expired or refresh token is invalid. Redirecting to login.")
|
||||
Log.e("API", "Session expired or refresh token is invalid..")
|
||||
|
||||
tokenManager.clearTokens()
|
||||
navigateToLoginActivity()
|
||||
|
||||
// This will make retrofit throw an HttpException, which we then catch from the
|
||||
// global exception handler where we redirect the user to the login activity.
|
||||
return Response.Builder()
|
||||
.request(chain.request())
|
||||
.protocol(Protocol.HTTP_1_1)
|
||||
|
@ -144,10 +145,4 @@ class AuthInterceptor(
|
|||
.body(ResponseBody.create(null, "Session expired"))
|
||||
.build()
|
||||
}
|
||||
|
||||
private fun navigateToLoginActivity() {
|
||||
val intent = Intent(context, LoginActivity::class.java)
|
||||
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
|
||||
context.startActivity(intent)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue