Prevent infinite loop on 403 and fix bad url used to load the terms

This commit is contained in:
Benoit Marty 2020-06-18 10:06:31 +02:00
parent ff39b22686
commit 0135368328
2 changed files with 7 additions and 3 deletions

View file

@ -139,7 +139,7 @@ class ReviewTermsViewModel @AssistedInject constructor(
)
}
} catch (failure: Throwable) {
Timber.e(failure, "Failed to agree to terms")
Timber.e(failure, "Failed to load terms")
setState {
copy(
termsList = Uninitialized

View file

@ -77,6 +77,9 @@ class WidgetViewModel @AssistedInject constructor(@Assisted val initialState: Wi
private val widgetURLFormatter = widgetService.getWidgetURLFormatter()
private val postAPIMediator = widgetService.getWidgetPostAPIMediator()
// Flag to avoid infinite loop
private var canRefreshToken = true
init {
integrationManagerService.addListener(this)
if (initialState.widgetKind.isAdmin()) {
@ -227,7 +230,7 @@ class WidgetViewModel @AssistedInject constructor(@Assisted val initialState: Wi
_viewEvents.post(WidgetViewEvents.LoadFormattedURL(formattedUrl))
} catch (failure: Throwable) {
if (failure is WidgetManagementFailure.TermsNotSignedException) {
_viewEvents.post(WidgetViewEvents.DisplayTerms(failure.baseUrl, failure.token))
_viewEvents.post(WidgetViewEvents.DisplayTerms(initialState.baseUrl, failure.token))
}
setState { copy(formattedURL = Fail(failure)) }
}
@ -251,7 +254,8 @@ class WidgetViewModel @AssistedInject constructor(@Assisted val initialState: Wi
}
if (action.isHttpError) {
// In case of 403, try to refresh the scalar token
if (it.formattedURL is Success && action.errorCode == HttpsURLConnection.HTTP_FORBIDDEN) {
if (it.formattedURL is Success && action.errorCode == HttpsURLConnection.HTTP_FORBIDDEN && canRefreshToken) {
canRefreshToken = false
loadFormattedUrl(true)
}
} else {