Skip to content

Commit 4662ecd

Browse files
committed
Cleanup Token Migration error logging.
1 parent cbdf7fa commit 4662ecd

2 files changed

Lines changed: 40 additions & 35 deletions

File tree

libs/SalesforceSDK/src/com/salesforce/androidsdk/accounts/UserAccountManagerExtension.kt

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
package com.salesforce.androidsdk.accounts
2828

2929
import android.content.Intent
30+
import com.salesforce.androidsdk.accounts.UserAccountManager.getInstance
3031
import com.salesforce.androidsdk.app.SalesforceSDKManager
3132
import com.salesforce.androidsdk.config.OAuthConfig
3233
import com.salesforce.androidsdk.ui.TokenMigrationActivity
@@ -43,36 +44,32 @@ const val TAG = "UserAccountManager"
4344
* new app. If successful a new set of credentials (refresh token, access token) are obtained
4445
* and replace the existing credentials for the user.
4546
*/
47+
@Suppress("UnusedReceiverParameter")
4648
fun UserAccountManager.migrateRefreshToken(
47-
userAccount: UserAccount? = UserAccountManager.getInstance().currentUser,
49+
userAccount: UserAccount? = getInstance().currentUser,
4850
appConfig: OAuthConfig,
4951
onMigrationSuccess: (userAccount: UserAccount) -> Unit,
5052
onMigrationError: (error: String, errorDesc: String?, e: Throwable?) -> Unit,
5153
) {
5254
val loggedOnSuccess: (userAccount: UserAccount) -> Unit = { user ->
53-
SalesforceSDKLogger.i(TAG, "User ($user) successfully migrated to " +
54-
"new OAuthConfig ($appConfig).")
55+
SalesforceSDKLogger.i(TAG, "Token Migration Successful \n\nUser ${user.username} " +
56+
"(${user.instanceServer}) successfully migrated to: \n$appConfig.")
5557
onMigrationSuccess.invoke(user)
5658
}
57-
val loggedOnError: (error: String, errorDesc: String?, e: Throwable?) -> Unit = { error, errorDesc, e ->
58-
val message = error + errorDesc?.let { "\nDescription: $it" }
59-
SalesforceSDKLogger.e(TAG, message, e)
60-
onMigrationError.invoke(error, errorDesc, e)
61-
}
59+
val userId = userAccount?.userId
60+
val orgId = userAccount?.orgId
6261

63-
val userId = userAccount?.userId ?: run {
64-
loggedOnError("User account or userId is null.", null, null)
65-
return
66-
}
67-
val orgId = userAccount.orgId ?: run {
68-
loggedOnError("OrgId is null.", null, null)
62+
if (userId == null || orgId == null) {
63+
val message = "User account, userId or orgId is null."
64+
SalesforceSDKLogger.e(TAG, message)
65+
onMigrationError(message, null, null)
6966
return
7067
}
7168

7269
val callbackKey = MigrationCallbackRegistry.register(
7370
callbacks = MigrationCallbackRegistry.MigrationCallbacks(
7471
onMigrationSuccess = loggedOnSuccess,
75-
onMigrationError = loggedOnError,
72+
onMigrationError = onMigrationError,
7673
)
7774
)
7875

libs/SalesforceSDK/src/com/salesforce/androidsdk/ui/TokenMigrationActivity.kt

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -107,28 +107,24 @@ internal class TokenMigrationActivity : ComponentActivity() {
107107

108108
// TODO: Move to non-deprecated getParcelableExtra when min API >= 33
109109
val oAuthConfig = intent.getParcelableExtra<OAuthConfig>(EXTRA_OAUTH_CONFIG) ?: run {
110-
resultCallback.onMigrationError("Unable to parse OAuthConfig.", null, null)
111-
finish()
110+
logMigrationError(resultCallback, "Unable to parse OAuthConfig.", null, null)
112111
return
113112
}
114113

115114
val orgId = intent.getStringExtra(EXTRA_ORG_ID)
116115
val userId = intent.getStringExtra(EXTRA_USER_ID)
117116
if ( orgId == null || userId == null) {
118-
resultCallback.onMigrationError("Unable to parse OAuthConfig.", null, null)
119-
finish()
117+
logMigrationError(resultCallback, "Unable to parse OAuthConfig.", null, null)
120118
return
121119
}
122120
val user = UserAccountManager.getInstance().getUserFromOrgAndUserId(orgId, userId) ?: run {
123-
resultCallback.onMigrationError("Unable to build user account.", null, null)
124-
finish()
121+
logMigrationError(resultCallback, "Unable to build user account.", null, null)
125122
return
126123
}
127124
val client = runCatching {
128125
SalesforceSDKManager.getInstance().clientManager.peekRestClient(user)
129126
}.getOrElse { e ->
130-
resultCallback.onMigrationError("Unable to build RestClient.", null, e as? Exception)
131-
finish()
127+
logMigrationError(resultCallback, "Unable to build RestClient.", null, e as? Exception)
132128
return
133129
}
134130

@@ -152,12 +148,12 @@ internal class TokenMigrationActivity : ComponentActivity() {
152148
}
153149
}.getOrNull()
154150
} ?: run {
155-
resultCallback.onMigrationError(
156-
/* error = */ "Request for single access bridge url failed.",
157-
/* errorDesc = */ "User's existing token may be invalid.",
158-
/* e = */ null,
151+
logMigrationError(
152+
resultCallback = resultCallback,
153+
error = "Request for single access bridge url failed",
154+
errorDesc = "User's existing token may be invalid.",
155+
e = null,
159156
)
160-
finish()
161157
return@launch
162158
}
163159
viewModel.dynamicBackgroundColor.value = Color.Transparent.copy(alpha = HALF_ALPHA)
@@ -231,12 +227,12 @@ internal class TokenMigrationActivity : ComponentActivity() {
231227
// Did we fail?
232228
when {
233229
error != null -> {
234-
resultCallback.onMigrationError(
235-
error,
236-
params["error_description"],
237-
null,
230+
logMigrationError(
231+
resultCallback = resultCallback,
232+
error = error,
233+
errorDesc = params["error_description"],
234+
e = null,
238235
)
239-
finish()
240236
}
241237

242238
else -> {
@@ -248,7 +244,7 @@ internal class TokenMigrationActivity : ComponentActivity() {
248244
when {
249245
viewModel.useWebServerFlow ->
250246
viewModel.onWebServerFlowComplete(
251-
params["code"],
247+
code = params["code"],
252248
onAuthFlowError = resultCallback.onMigrationError,
253249
onAuthFlowSuccess = resultCallback.onMigrationSuccess,
254250
loginServer = instanceServer,
@@ -257,7 +253,7 @@ internal class TokenMigrationActivity : ComponentActivity() {
257253

258254
else ->
259255
viewModel.onAuthFlowComplete(
260-
TokenEndpointResponse(params),
256+
tr = TokenEndpointResponse(params),
261257
onAuthFlowError = resultCallback.onMigrationError,
262258
onAuthFlowSuccess = resultCallback.onMigrationSuccess,
263259
tokenMigration = true,
@@ -300,6 +296,18 @@ internal class TokenMigrationActivity : ComponentActivity() {
300296
loadUrl(frontDoorUrl)
301297
}
302298

299+
private fun logMigrationError(
300+
resultCallback: MigrationCallbackRegistry.MigrationCallbacks,
301+
error: String,
302+
errorDesc: String?,
303+
e: Throwable?,
304+
) {
305+
val message = error + (errorDesc?.let { ": $it" } ?: "")
306+
SalesforceSDKLogger.e(TAG, message, e)
307+
resultCallback.onMigrationError(error, errorDesc, e)
308+
finish()
309+
}
310+
303311
// Ensure Status Bar Icons are readable no matter which OS theme is used.
304312
private fun makeStatusBarVisible() {
305313
val usingDarkTheme = viewModel.dynamicBackgroundTheme.value == DARK

0 commit comments

Comments
 (0)