From 4061c7450b7ba0fb74d178e4a4876b06255b34c1 Mon Sep 17 00:00:00 2001 From: len Date: Mon, 12 Dec 2016 20:53:44 +0100 Subject: [PATCH] Better network error handling --- .../data/network/OkHttpExtensions.kt | 9 +++++++ .../data/source/online/OnlineSource.kt | 27 +++++++------------ 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/network/OkHttpExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/data/network/OkHttpExtensions.kt index aa0d8d6ad4..8850c362d7 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/network/OkHttpExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/network/OkHttpExtensions.kt @@ -46,6 +46,15 @@ fun Call.asObservable(): Observable { } } +fun Call.asObservableSuccess(): Observable { + return asObservable().doOnNext { response -> + if (!response.isSuccessful) { + response.close() + throw Exception("Unsuccessful code ${response.code()}") + } + } +} + fun OkHttpClient.newCallWithProgress(request: Request, listener: ProgressListener): Call { val progressClient = newBuilder() .cache(null) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/source/online/OnlineSource.kt b/app/src/main/java/eu/kanade/tachiyomi/data/source/online/OnlineSource.kt index 71152a0486..2746084477 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/source/online/OnlineSource.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/source/online/OnlineSource.kt @@ -6,7 +6,7 @@ import eu.kanade.tachiyomi.data.database.models.Chapter import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.data.network.GET import eu.kanade.tachiyomi.data.network.NetworkHelper -import eu.kanade.tachiyomi.data.network.asObservable +import eu.kanade.tachiyomi.data.network.asObservableSuccess import eu.kanade.tachiyomi.data.network.newCallWithProgress import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.source.Language @@ -93,7 +93,7 @@ abstract class OnlineSource() : Source { */ open fun fetchPopularManga(page: MangasPage): Observable = client .newCall(popularMangaRequest(page)) - .asObservable() + .asObservableSuccess() .map { response -> popularMangaParse(response, page) page @@ -136,7 +136,7 @@ abstract class OnlineSource() : Source { */ open fun fetchSearchManga(page: MangasPage, query: String, filters: List): Observable = client .newCall(searchMangaRequest(page, query, filters)) - .asObservable() + .asObservableSuccess() .map { response -> searchMangaParse(response, page, query, filters) page @@ -178,7 +178,7 @@ abstract class OnlineSource() : Source { */ open fun fetchLatestUpdates(page: MangasPage): Observable = client .newCall(latestUpdatesRequest(page)) - .asObservable() + .asObservableSuccess() .map { response -> latestUpdatesParse(response, page) page @@ -212,7 +212,7 @@ abstract class OnlineSource() : Source { */ override fun fetchMangaDetails(manga: Manga): Observable = client .newCall(mangaDetailsRequest(manga)) - .asObservable() + .asObservableSuccess() .map { response -> Manga.create(manga.url, id).apply { mangaDetailsParse(response, this) @@ -246,7 +246,7 @@ abstract class OnlineSource() : Source { */ override fun fetchChapterList(manga: Manga): Observable> = client .newCall(chapterListRequest(manga)) - .asObservable() + .asObservableSuccess() .map { response -> mutableListOf().apply { chapterListParse(response, this) @@ -292,11 +292,8 @@ abstract class OnlineSource() : Source { */ open fun fetchPageListFromNetwork(chapter: Chapter): Observable> = client .newCall(pageListRequest(chapter)) - .asObservable() + .asObservableSuccess() .map { response -> - if (!response.isSuccessful) { - throw Exception("Webpage sent ${response.code()} code") - } mutableListOf().apply { pageListParse(response, this) if (isEmpty()) { @@ -338,7 +335,7 @@ abstract class OnlineSource() : Source { page.status = Page.LOAD_PAGE return client .newCall(imageUrlRequest(page)) - .asObservable() + .asObservableSuccess() .map { imageUrlParse(it) } .doOnError { page.status = Page.ERROR } .onErrorReturn { null } @@ -381,13 +378,7 @@ abstract class OnlineSource() : Source { */ fun imageResponse(page: Page): Observable = client .newCallWithProgress(imageRequest(page), page) - .asObservable() - .doOnNext { - if (!it.isSuccessful) { - it.close() - throw RuntimeException("Not a valid response") - } - } + .asObservableSuccess() /** * Returns the request for getting the source image. Override only if it's needed to override