From f756dc13e494bae02ca9e9d82a785652e0117a68 Mon Sep 17 00:00:00 2001 From: "maarten.vercruysse" Date: Tue, 25 Jul 2023 20:22:58 +0200 Subject: [PATCH] Prevent `Value X cannot be converted` from showing --- app/src/main/java/com/jerboa/api/Http.kt | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/jerboa/api/Http.kt b/app/src/main/java/com/jerboa/api/Http.kt index 3b59979f5..aab495c23 100644 --- a/app/src/main/java/com/jerboa/api/Http.kt +++ b/app/src/main/java/com/jerboa/api/Http.kt @@ -14,6 +14,7 @@ import okhttp3.OkHttpClient import okhttp3.Protocol import okhttp3.RequestBody.Companion.toRequestBody import okhttp3.ResponseBody.Companion.toResponseBody +import org.json.JSONException import org.json.JSONObject import retrofit2.Response import retrofit2.Retrofit @@ -395,10 +396,12 @@ fun retrofitErrorHandler(res: Response): T { return res.body()!! } else { val errMsg = res.errorBody()?.string()?.let { - JSONObject(it).getString("error") - } ?: run { - res.code().toString() - } + try { // Prevent Could not convert to JSON messages everywhere + JSONObject(it).getString("error") + } catch (_: JSONException) { + it + } + } ?: res.code().toString() throw Exception(errMsg) }