Skip to content

Commit 6620b13

Browse files
Merge pull request #48 from twin-te/handle-network-exceptions
Handle `java.lang.IOException` with network requests
2 parents e9aad58 + 2973325 commit 6620b13

File tree

4 files changed

+47
-28
lines changed

4 files changed

+47
-28
lines changed

app/src/main/java/net/twinte/android/MainActivity.kt

+25-12
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import android.webkit.ValueCallback
1111
import android.webkit.WebChromeClient
1212
import android.webkit.WebResourceRequest
1313
import android.webkit.WebView
14+
import android.widget.Toast
1415
import androidx.appcompat.app.AppCompatActivity
1516
import androidx.fragment.app.Fragment
1617
import androidx.webkit.WebSettingsCompat
@@ -72,13 +73,17 @@ class MainActivity : AppCompatActivity(), SubWebViewFragment.Callback {
7273
UpdateScheduleWorker.scheduleNextUpdate(workManager)
7374
scheduleNotificationDataStore.schedule()
7475
GlobalScope.launch {
75-
try {
76-
scheduleDataStore.update()
77-
} catch (e: NotLoggedInException) {
78-
// 未ログイン時は失敗するが何もしない
79-
} catch (e: Exception) {
80-
// それ以外の予期せぬエラー
81-
}
76+
kotlin.runCatching { scheduleDataStore.update() }
77+
.fold(onSuccess = {}, onFailure = {
78+
when (it) {
79+
is NotLoggedInException -> {
80+
// 未ログイン時は失敗するが何もしない
81+
}
82+
else -> {
83+
// それ以外の予期せぬエラー
84+
}
85+
}
86+
})
8287
WidgetUpdater.updateAllWidget(this@MainActivity)
8388
WidgetUpdater.scheduleAllIfExists(this@MainActivity)
8489
}
@@ -197,7 +202,10 @@ class MainActivity : AppCompatActivity(), SubWebViewFragment.Callback {
197202
val account = GoogleSignIn.getSignedInAccountFromIntent(data).result
198203
GlobalScope.launch {
199204
account?.idToken?.let {
200-
userDataStore.validateGoogleIdToken(it)
205+
kotlin.runCatching { userDataStore.validateGoogleIdToken(it) }
206+
.onFailure {
207+
Toast.makeText(applicationContext, R.string.common_google_play_services_unknown_issue, Toast.LENGTH_SHORT).show()
208+
}
201209
}
202210
withContext(Dispatchers.Main) {
203211
binding.mainWebview.loadUrl(twinteUrlBuilder(serverSettings).buildUrl())
@@ -219,11 +227,16 @@ class MainActivity : AppCompatActivity(), SubWebViewFragment.Callback {
219227
super.onPause()
220228
cookieManager.flush()
221229
GlobalScope.launch {
222-
try {
230+
kotlin.runCatching {
223231
scheduleDataStore.update()
224-
} catch (e: NotLoggedInException) {
225-
// 未ログイン時は失敗するが何もしない
226-
}
232+
}.fold(onSuccess = {}, onFailure = { e ->
233+
when (e) {
234+
is NotLoggedInException -> {
235+
// 未ログイン時は失敗するが何もしない
236+
}
237+
else -> { }
238+
}
239+
})
227240
WidgetUpdater.updateAllWidget(this@MainActivity)
228241
}
229242
}

app/src/main/java/net/twinte/android/datastore/schedule/SharedPreferencesScheduleDataStore.kt

+16-12
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,22 @@ class SharedPreferencesScheduleDataStore @Inject constructor(
2626
with(pref.edit()) {
2727
clear()
2828
calendar.map { simpleDateFormat.format(it) }.forEach { d ->
29-
val res = twinteBackendHttpClient.get("/api/v3/timetable/$d")
30-
31-
if (!res.isSuccessful) {
32-
if (res.code == 401) {
33-
throw NotLoggedInException()
34-
} else {
35-
throw IOException("API call failed with code ${res.code}\n ${res.body?.string()}")
36-
}
37-
}
38-
putString(d, res.body?.string())
39-
// TODO: replace `android.util.Log` with Timber
40-
// Log.d(TAG, "schedule updated $d $res")
29+
kotlin.runCatching { twinteBackendHttpClient.get("/api/v3/timetable/$d") }
30+
.fold(onSuccess = {
31+
if (!it.isSuccessful) {
32+
if (it.code == 401) {
33+
throw NotLoggedInException()
34+
} else {
35+
throw IOException("API call failed with code ${it.code}\n ${it.body.string()}")
36+
}
37+
}
38+
putString(d, it.body.string())
39+
}, onFailure = {
40+
if (it !is IOException) {
41+
throw it
42+
}
43+
return@withContext
44+
})
4145
}
4246
commit()
4347
}

app/src/main/java/net/twinte/android/widget/V3LargeWidgetProvider.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,11 @@ class V3LargeWidgetRemoteViewService @Inject constructor() : RemoteViewsService(
139139

140140
override fun onCreate() {}
141141

142-
override fun onDataSetChanged() = runBlocking {
142+
override fun onDataSetChanged(): Unit = runBlocking {
143143
Log.d("LargeFactory", "onDataSetChanged")
144144
val (current, _) = WidgetUpdater.getShouldShowCurrentDate()
145-
schedule = scheduleDataStore.getSchedule(current.time)
145+
kotlin.runCatching { scheduleDataStore.getSchedule(current.time) }
146+
.onSuccess { schedule = it }
146147
}
147148

148149
override fun onDestroy() {}

app/src/main/java/net/twinte/android/widget/V3MediumWidgetProvider.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,11 @@ class V3MediumWidgetRemoteViewService @Inject constructor() : RemoteViewsService
138138

139139
override fun onCreate() {}
140140

141-
override fun onDataSetChanged() = runBlocking {
141+
override fun onDataSetChanged(): Unit = runBlocking {
142142
Log.d("MediumFactory", "onDataSetChanged")
143143
val (current, _) = WidgetUpdater.getShouldShowCurrentDate()
144-
schedule = scheduleDataStore.getSchedule(current.time)
144+
kotlin.runCatching { scheduleDataStore.getSchedule(current.time) }
145+
.onSuccess { schedule = it }
145146
}
146147

147148
override fun onDestroy() {}

0 commit comments

Comments
 (0)