Skip to content

Commit d45812c

Browse files
Improve error handling code to match master branch
1 parent 125468a commit d45812c

File tree

2 files changed

+17
-28
lines changed

2 files changed

+17
-28
lines changed

app/src/main/java/guide/graphql/toc/ui/chapters/ChaptersViewModel.kt

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package guide.graphql.toc.ui.chapters
22

3-
import android.util.Log
43
import androidx.lifecycle.LiveData
54
import androidx.lifecycle.ViewModel
65
import androidx.lifecycle.liveData
@@ -20,19 +19,15 @@ class ChaptersViewModel : ViewModel() {
2019
).toDeferred().await()
2120

2221
if (response.hasErrors()) {
23-
emit(Resource.error("Response has errors", null))
24-
return@liveData
22+
throw Exception("Response has errors")
2523
}
26-
response.data?.chapters?.let {
27-
emit(Resource.success(response.data!!.chapters))
28-
return@liveData
29-
}
30-
emit(Resource.error("Data is null", null))
31-
return@liveData
24+
25+
val chapters = response.data?.chapters ?: throw Exception("Data is null")
26+
emit(Resource.success(chapters))
3227
} catch (e: ApolloException) {
33-
Log.d("ChaptersQuery", "GraphQL request failed", e)
3428
emit(Resource.error("GraphQL request failed", null))
35-
return@liveData
29+
} catch (e: Exception) {
30+
emit(Resource.error(e.message.orEmpty(), null))
3631
}
3732
}
3833
}

app/src/main/java/guide/graphql/toc/ui/sections/SectionsViewModel.kt

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package guide.graphql.toc.ui.sections
22

3-
import android.util.Log
43
import androidx.lifecycle.*
54
import com.apollographql.apollo.coroutines.toDeferred
65
import com.apollographql.apollo.exception.ApolloException
7-
import guide.graphql.toc.data.Resource
86
import guide.graphql.toc.SectionsQuery
7+
import guide.graphql.toc.data.Resource
98
import guide.graphql.toc.data.apolloClient
109

1110
class SectionsViewModel : ViewModel() {
@@ -23,32 +22,27 @@ class SectionsViewModel : ViewModel() {
2322
}
2423

2524
val sectionsList: LiveData<Resource<List<SectionsQuery.Section?>>> =
26-
_chapterId.switchMap { sectionId ->
25+
_chapterId.switchMap { chapterId ->
2726
return@switchMap liveData {
2827
emit(Resource.loading(null))
2928
try {
3029
val response = apolloClient.query(
31-
SectionsQuery(id = sectionId)
30+
SectionsQuery(id = chapterId)
3231
).toDeferred().await()
3332

3433
if (response.hasErrors()) {
35-
emit(Resource.error("Response has errors", null))
36-
return@liveData
34+
throw Exception("Response has errors")
3735
}
38-
response.data?.chapter?.sections?.let { sections ->
39-
if (sections.size > 1) {
40-
emit(Resource.success(sections))
41-
} else {
42-
emit(Resource.error("No sections", null))
43-
}
44-
return@liveData
36+
val sections = response.data?.chapter?.sections ?: throw Exception("Data is null")
37+
if (sections.size > 1) {
38+
emit(Resource.success(sections))
39+
} else {
40+
throw Exception("No sections")
4541
}
46-
emit(Resource.error("Chapter has no sections", null))
47-
return@liveData
4842
} catch (e: ApolloException) {
49-
Log.d("Sections Query", "GraphQL request failed", e)
5043
emit(Resource.error("GraphQL request failed", null))
51-
return@liveData
44+
} catch (e: Exception) {
45+
emit(Resource.error(e.message.orEmpty(), null))
5246
}
5347
}
5448
}

0 commit comments

Comments
 (0)