mirror of
https://github.com/mihonapp/mihon.git
synced 2024-11-07 19:27:25 +01:00
Better network error handling
This commit is contained in:
parent
9ad535bde6
commit
4061c7450b
@ -46,6 +46,15 @@ fun Call.asObservable(): Observable<Response> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun Call.asObservableSuccess(): Observable<Response> {
|
||||||
|
return asObservable().doOnNext { response ->
|
||||||
|
if (!response.isSuccessful) {
|
||||||
|
response.close()
|
||||||
|
throw Exception("Unsuccessful code ${response.code()}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fun OkHttpClient.newCallWithProgress(request: Request, listener: ProgressListener): Call {
|
fun OkHttpClient.newCallWithProgress(request: Request, listener: ProgressListener): Call {
|
||||||
val progressClient = newBuilder()
|
val progressClient = newBuilder()
|
||||||
.cache(null)
|
.cache(null)
|
||||||
|
@ -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.database.models.Manga
|
||||||
import eu.kanade.tachiyomi.data.network.GET
|
import eu.kanade.tachiyomi.data.network.GET
|
||||||
import eu.kanade.tachiyomi.data.network.NetworkHelper
|
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.network.newCallWithProgress
|
||||||
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
|
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
|
||||||
import eu.kanade.tachiyomi.data.source.Language
|
import eu.kanade.tachiyomi.data.source.Language
|
||||||
@ -93,7 +93,7 @@ abstract class OnlineSource() : Source {
|
|||||||
*/
|
*/
|
||||||
open fun fetchPopularManga(page: MangasPage): Observable<MangasPage> = client
|
open fun fetchPopularManga(page: MangasPage): Observable<MangasPage> = client
|
||||||
.newCall(popularMangaRequest(page))
|
.newCall(popularMangaRequest(page))
|
||||||
.asObservable()
|
.asObservableSuccess()
|
||||||
.map { response ->
|
.map { response ->
|
||||||
popularMangaParse(response, page)
|
popularMangaParse(response, page)
|
||||||
page
|
page
|
||||||
@ -136,7 +136,7 @@ abstract class OnlineSource() : Source {
|
|||||||
*/
|
*/
|
||||||
open fun fetchSearchManga(page: MangasPage, query: String, filters: List<Filter>): Observable<MangasPage> = client
|
open fun fetchSearchManga(page: MangasPage, query: String, filters: List<Filter>): Observable<MangasPage> = client
|
||||||
.newCall(searchMangaRequest(page, query, filters))
|
.newCall(searchMangaRequest(page, query, filters))
|
||||||
.asObservable()
|
.asObservableSuccess()
|
||||||
.map { response ->
|
.map { response ->
|
||||||
searchMangaParse(response, page, query, filters)
|
searchMangaParse(response, page, query, filters)
|
||||||
page
|
page
|
||||||
@ -178,7 +178,7 @@ abstract class OnlineSource() : Source {
|
|||||||
*/
|
*/
|
||||||
open fun fetchLatestUpdates(page: MangasPage): Observable<MangasPage> = client
|
open fun fetchLatestUpdates(page: MangasPage): Observable<MangasPage> = client
|
||||||
.newCall(latestUpdatesRequest(page))
|
.newCall(latestUpdatesRequest(page))
|
||||||
.asObservable()
|
.asObservableSuccess()
|
||||||
.map { response ->
|
.map { response ->
|
||||||
latestUpdatesParse(response, page)
|
latestUpdatesParse(response, page)
|
||||||
page
|
page
|
||||||
@ -212,7 +212,7 @@ abstract class OnlineSource() : Source {
|
|||||||
*/
|
*/
|
||||||
override fun fetchMangaDetails(manga: Manga): Observable<Manga> = client
|
override fun fetchMangaDetails(manga: Manga): Observable<Manga> = client
|
||||||
.newCall(mangaDetailsRequest(manga))
|
.newCall(mangaDetailsRequest(manga))
|
||||||
.asObservable()
|
.asObservableSuccess()
|
||||||
.map { response ->
|
.map { response ->
|
||||||
Manga.create(manga.url, id).apply {
|
Manga.create(manga.url, id).apply {
|
||||||
mangaDetailsParse(response, this)
|
mangaDetailsParse(response, this)
|
||||||
@ -246,7 +246,7 @@ abstract class OnlineSource() : Source {
|
|||||||
*/
|
*/
|
||||||
override fun fetchChapterList(manga: Manga): Observable<List<Chapter>> = client
|
override fun fetchChapterList(manga: Manga): Observable<List<Chapter>> = client
|
||||||
.newCall(chapterListRequest(manga))
|
.newCall(chapterListRequest(manga))
|
||||||
.asObservable()
|
.asObservableSuccess()
|
||||||
.map { response ->
|
.map { response ->
|
||||||
mutableListOf<Chapter>().apply {
|
mutableListOf<Chapter>().apply {
|
||||||
chapterListParse(response, this)
|
chapterListParse(response, this)
|
||||||
@ -292,11 +292,8 @@ abstract class OnlineSource() : Source {
|
|||||||
*/
|
*/
|
||||||
open fun fetchPageListFromNetwork(chapter: Chapter): Observable<List<Page>> = client
|
open fun fetchPageListFromNetwork(chapter: Chapter): Observable<List<Page>> = client
|
||||||
.newCall(pageListRequest(chapter))
|
.newCall(pageListRequest(chapter))
|
||||||
.asObservable()
|
.asObservableSuccess()
|
||||||
.map { response ->
|
.map { response ->
|
||||||
if (!response.isSuccessful) {
|
|
||||||
throw Exception("Webpage sent ${response.code()} code")
|
|
||||||
}
|
|
||||||
mutableListOf<Page>().apply {
|
mutableListOf<Page>().apply {
|
||||||
pageListParse(response, this)
|
pageListParse(response, this)
|
||||||
if (isEmpty()) {
|
if (isEmpty()) {
|
||||||
@ -338,7 +335,7 @@ abstract class OnlineSource() : Source {
|
|||||||
page.status = Page.LOAD_PAGE
|
page.status = Page.LOAD_PAGE
|
||||||
return client
|
return client
|
||||||
.newCall(imageUrlRequest(page))
|
.newCall(imageUrlRequest(page))
|
||||||
.asObservable()
|
.asObservableSuccess()
|
||||||
.map { imageUrlParse(it) }
|
.map { imageUrlParse(it) }
|
||||||
.doOnError { page.status = Page.ERROR }
|
.doOnError { page.status = Page.ERROR }
|
||||||
.onErrorReturn { null }
|
.onErrorReturn { null }
|
||||||
@ -381,13 +378,7 @@ abstract class OnlineSource() : Source {
|
|||||||
*/
|
*/
|
||||||
fun imageResponse(page: Page): Observable<Response> = client
|
fun imageResponse(page: Page): Observable<Response> = client
|
||||||
.newCallWithProgress(imageRequest(page), page)
|
.newCallWithProgress(imageRequest(page), page)
|
||||||
.asObservable()
|
.asObservableSuccess()
|
||||||
.doOnNext {
|
|
||||||
if (!it.isSuccessful) {
|
|
||||||
it.close()
|
|
||||||
throw RuntimeException("Not a valid response")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the request for getting the source image. Override only if it's needed to override
|
* Returns the request for getting the source image. Override only if it's needed to override
|
||||||
|
Loading…
Reference in New Issue
Block a user