From 10aaeb245e2bf743bf113746c0a4148e013e20ad Mon Sep 17 00:00:00 2001
From: Alessandro Jean <14254807+alessandrojean@users.noreply.github.com>
Date: Fri, 29 Apr 2022 19:41:58 -0300
Subject: [PATCH] Show better Google Drive error messages on TS. (#11663)

---
 src/pt/taosect/build.gradle                   |  2 +-
 .../tachiyomi/extension/pt/taosect/TaoSect.kt | 27 ++++++++++++++++---
 2 files changed, 24 insertions(+), 5 deletions(-)

diff --git a/src/pt/taosect/build.gradle b/src/pt/taosect/build.gradle
index 993b1545c..89641a142 100644
--- a/src/pt/taosect/build.gradle
+++ b/src/pt/taosect/build.gradle
@@ -6,7 +6,7 @@ ext {
     extName = 'Tao Sect'
     pkgNameSuffix = 'pt.taosect'
     extClass = '.TaoSect'
-    extVersionCode = 15
+    extVersionCode = 16
     isNsfw = true
 }
 
diff --git a/src/pt/taosect/src/eu/kanade/tachiyomi/extension/pt/taosect/TaoSect.kt b/src/pt/taosect/src/eu/kanade/tachiyomi/extension/pt/taosect/TaoSect.kt
index 302cc581c..d5720d1ad 100644
--- a/src/pt/taosect/src/eu/kanade/tachiyomi/extension/pt/taosect/TaoSect.kt
+++ b/src/pt/taosect/src/eu/kanade/tachiyomi/extension/pt/taosect/TaoSect.kt
@@ -289,12 +289,17 @@ class TaoSect : HttpSource() {
             val firstPageRequest = imageRequest(firstPage)
 
             client.newCall(firstPageRequest).execute().use {
-                it.headers["Content-Type"]!!.contains("text/html")
+                val isHtml = it.headers["Content-Type"]!!.contains("text/html")
+
+                GoogleDriveResponse(!isHtml && it.isSuccessful, it.code)
             }
         }
 
-        if (hasExceededViewLimit.getOrDefault(false)) {
-            throw Exception(EXCEEDED_GOOGLE_DRIVE_VIEW_LIMIT)
+        val defaultResponse = GoogleDriveResponse(false, GD_BACKEND_ERROR)
+        val googleDriveResponse = hasExceededViewLimit.getOrDefault(defaultResponse)
+
+        if (!googleDriveResponse.isValid) {
+            throw Exception(googleDriveResponse.errorMessage)
         }
 
         return pages
@@ -401,6 +406,14 @@ class TaoSect : HttpSource() {
         Tag("62", "Webtoon")
     )
 
+    private data class GoogleDriveResponse(val isValid: Boolean, val code: Int) {
+        val errorMessage: String
+            get() = when (code) {
+                GD_SHARING_RATE_LIMIT_EXCEEDED -> EXCEEDED_GOOGLE_DRIVE_VIEW_LIMIT
+                else -> GOOGLE_DRIVE_UNAVAILABLE
+            }
+    }
+
     companion object {
         private const val ACCEPT_IMAGE = "image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8"
         private const val ACCEPT_JSON = "application/json"
@@ -415,7 +428,13 @@ class TaoSect : HttpSource() {
         private const val PROJECT_NOT_FOUND = "Projeto não encontrado."
         private const val CHAPTERS_NOT_FOUND = "Capítulos não encontrados."
         private const val EXCEEDED_GOOGLE_DRIVE_VIEW_LIMIT = "Limite de visualizações atingido " +
-            "no Google Drive. Aguarde com que o limite seja reestabelecido."
+            "no Google Drive. Tente novamente mais tarde."
+        private const val GOOGLE_DRIVE_UNAVAILABLE = "O Google Drive está indisponível no " +
+            "momento. Tente novamente mais tarde."
+
+        // Reference: https://developers.google.com/drive/api/guides/handle-errors
+        private const val GD_SHARING_RATE_LIMIT_EXCEEDED = 403
+        private const val GD_BACKEND_ERROR = 500
 
         private val DATE_FORMATTER by lazy {
             SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.ENGLISH)