From c0cef58e39a70b8d29ce12a05f7276bec14b0df2 Mon Sep 17 00:00:00 2001 From: arkon Date: Thu, 24 Dec 2020 15:09:10 -0500 Subject: [PATCH] Surface MAL HTTP errors properly --- .../data/track/myanimelist/MyAnimeList.kt | 13 +++++++------ .../data/track/myanimelist/MyAnimeListApi.kt | 14 +++++++------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeList.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeList.kt index 029bc09c1..9191e4c8c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeList.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeList.kt @@ -91,17 +91,18 @@ class MyAnimeList(private val context: Context, id: Int) : TrackService(id) { override fun login(username: String, password: String) = login(password) fun login(authCode: String): Completable { - return try { + return Completable.fromCallable { val oauth = runBlocking { api.getAccessToken(authCode) } interceptor.setAuth(oauth) val username = runBlocking { api.getCurrentUser() } saveCredentials(username, oauth.access_token) - return Completable.complete() - } catch (e: Exception) { - Timber.e(e) - logout() - Completable.error(e) } + .doOnError { + Timber.e(it) + logout() + } + .subscribeOn(Schedulers.io()) + .observeOn(AndroidSchedulers.mainThread()) } override fun logout() { diff --git a/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeListApi.kt b/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeListApi.kt index cd26b51ee..65dac87a2 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeListApi.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/data/track/myanimelist/MyAnimeListApi.kt @@ -44,7 +44,7 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI .add("code_verifier", codeVerifier) .add("grant_type", "authorization_code") .build() - client.newCall(POST("$baseOAuthUrl/token", body = formBody)).await().use { + client.newCall(POST("$baseOAuthUrl/token", body = formBody)).await(assertSuccess = true).use { val responseBody = it.body?.string().orEmpty() json.decodeFromString(responseBody) } @@ -57,7 +57,7 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI .url("$baseApiUrl/users/@me") .get() .build() - authClient.newCall(request).await().use { + authClient.newCall(request).await(assertSuccess = true).use { val responseBody = it.body?.string().orEmpty() val response = json.decodeFromString(responseBody) response["name"]!!.jsonPrimitive.content @@ -70,7 +70,7 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI val url = "$baseApiUrl/manga".toUri().buildUpon() .appendQueryParameter("q", query) .build() - authClient.newCall(GET(url.toString())).await().use { + authClient.newCall(GET(url.toString())).await(assertSuccess = true).use { val responseBody = it.body?.string().orEmpty() val response = json.decodeFromString(responseBody) response["data"]!!.jsonArray @@ -91,7 +91,7 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI .appendPath(id.toString()) .appendQueryParameter("fields", "id,title,synopsis,num_chapters,main_picture,status,media_type,start_date") .build() - authClient.newCall(GET(url.toString())).await().use { + authClient.newCall(GET(url.toString())).await(assertSuccess = true).use { val responseBody = it.body?.string().orEmpty() val response = json.decodeFromString(responseBody) val obj = response.jsonObject @@ -124,7 +124,7 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI .url(mangaUrl(track.media_id).toString()) .put(formBody) .build() - authClient.newCall(request).await().use { + authClient.newCall(request).await(assertSuccess = true).use { parseMangaItem(it, track) } } @@ -140,7 +140,7 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI .url(mangaUrl(track.media_id).toString()) .put(formBody) .build() - authClient.newCall(request).await().use { + authClient.newCall(request).await(assertSuccess = true).use { parseMangaItem(it, track) } } @@ -158,7 +158,7 @@ class MyAnimeListApi(private val client: OkHttpClient, interceptor: MyAnimeListI .url(mangaUrl(track.media_id).toString()) .put(formBody) .build() - authClient.newCall(request).await().use { + authClient.newCall(request).await(assertSuccess = true).use { parseMangaItem(it, track) } }