Skip to content

Commit e320666

Browse files
committed
Error handling fixes
1 parent 059b400 commit e320666

2 files changed

Lines changed: 35 additions & 20 deletions

File tree

app/src/main/java/com/cornellappdev/coursegrab/LoginActivity.kt

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ import com.cornellappdev.coursegrab.networking.deviceToken
2626
import com.cornellappdev.coursegrab.networking.initializeSession
2727
import com.cornellappdev.coursegrab.networking.setNotification
2828
import com.cornellappdev.coursegrab.networking.updateSession
29-
import com.google.android.gms.common.api.ApiException
3029
import com.google.android.libraries.identity.googleid.GetGoogleIdOption
3130
import com.google.android.libraries.identity.googleid.GetSignInWithGoogleOption
3231
import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential
3332
import com.google.android.libraries.identity.googleid.GoogleIdTokenParsingException
3433
import com.google.android.material.snackbar.Snackbar
3534
import com.google.firebase.messaging.FirebaseMessaging
3635
import com.google.gson.reflect.TypeToken
36+
import kotlinx.coroutines.CancellationException
3737
import kotlinx.coroutines.Dispatchers
3838
import kotlinx.coroutines.launch
3939
import kotlinx.coroutines.withContext
@@ -61,26 +61,29 @@ class LoginActivity : AppCompatActivity() {
6161
val intent = Intent(this@LoginActivity, MainActivity::class.java)
6262
startActivity(intent)
6363
} else {
64-
if (preferencesHelper.updateToken != null)
65-
try {
66-
val updateSession =
67-
Endpoint.updateSession(preferencesHelper.updateToken.toString())
64+
val updateToken = preferencesHelper.updateToken
65+
if (!updateToken.isNullOrBlank()) {
66+
val updateSession = Endpoint.updateSession(updateToken)
6867

69-
lifecycleScope.launch {
68+
lifecycleScope.launch {
69+
val userSession = try {
7070
val typeToken = object : TypeToken<ApiResponse<UserSession>>() {}.type
71-
val userSession = withContext(Dispatchers.IO) {
71+
withContext(Dispatchers.IO) {
7272
Request.makeRequest<ApiResponse<UserSession>>(
7373
updateSession.okHttpRequest(),
7474
typeToken
7575
)
76-
}!!.data
77-
78-
verifySession(userSession)
76+
}?.data
77+
} catch (e: CancellationException) {
78+
throw e
79+
} catch (e: Exception) {
80+
Log.d(TAG, "Could not resume previous session", e)
81+
null
7982
}
8083

81-
} catch (e: ApiException) {
82-
e.printStackTrace()
84+
if (userSession != null) verifySession(userSession)
8385
}
86+
}
8487
}
8588

8689
binding.signInButton.setOnClickListener { signIn() }
@@ -190,13 +193,25 @@ class LoginActivity : AppCompatActivity() {
190193
val initializeSession = Endpoint.initializeSession(googleCredential.idToken, null)
191194

192195
lifecycleScope.launch {
193-
val typeToken = object : TypeToken<ApiResponse<UserSession>>() {}.type
194-
val userSession = withContext(Dispatchers.IO) {
195-
Request.makeRequest<ApiResponse<UserSession>>(
196-
initializeSession.okHttpRequest(),
197-
typeToken
198-
)
199-
}!!.data
196+
val userSession = try {
197+
val typeToken = object : TypeToken<ApiResponse<UserSession>>() {}.type
198+
withContext(Dispatchers.IO) {
199+
Request.makeRequest<ApiResponse<UserSession>>(
200+
initializeSession.okHttpRequest(),
201+
typeToken
202+
)
203+
}?.data
204+
} catch (e: CancellationException) {
205+
throw e
206+
} catch (e: Exception) {
207+
Log.e(TAG, "Failed to initialize session", e)
208+
null
209+
}
210+
211+
if (userSession == null) {
212+
showLoginError("Sign-in failed. Please try again.")
213+
return@launch
214+
}
200215

201216
verifySession(userSession)
202217
}

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ buildscript {
99
dependencies {
1010
classpath 'com.android.tools.build:gradle:9.3.2'
1111
classpath 'com.google.gms:google-services:4.5.0'
12-
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:2.2.10'
12+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1313
classpath 'com.google.firebase:firebase-crashlytics-gradle:3.0.8'
1414
// NOTE: Do not place your application dependencies here; they belong
1515
// in the individual module build.gradle files

0 commit comments

Comments
 (0)