Fix handling sync server errors
This commit is contained in:
@@ -269,4 +269,4 @@ class SyncHelper(
|
||||
private fun JSONObject.removeJSONObject(name: String) = remove(name) as JSONObject
|
||||
|
||||
private fun JSONObject.removeJSONArray(name: String) = remove(name) as JSONArray
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,9 @@ package org.koitharu.kotatsu.utils.ext
|
||||
import okhttp3.MediaType.Companion.toMediaType
|
||||
import okhttp3.RequestBody.Companion.toRequestBody
|
||||
import okhttp3.Response
|
||||
import okhttp3.internal.closeQuietly
|
||||
import okio.IOException
|
||||
import org.json.JSONObject
|
||||
import org.koitharu.kotatsu.parsers.util.parseJson
|
||||
import java.net.HttpURLConnection
|
||||
|
||||
private val TYPE_JSON = "application/json".toMediaType()
|
||||
@@ -12,9 +13,13 @@ private val TYPE_JSON = "application/json".toMediaType()
|
||||
fun JSONObject.toRequestBody() = toString().toRequestBody(TYPE_JSON)
|
||||
|
||||
fun Response.parseJsonOrNull(): JSONObject? {
|
||||
return if (code == HttpURLConnection.HTTP_NO_CONTENT) {
|
||||
null
|
||||
} else {
|
||||
parseJson()
|
||||
return try {
|
||||
when {
|
||||
!isSuccessful -> throw IOException(body?.string())
|
||||
code == HttpURLConnection.HTTP_NO_CONTENT -> null
|
||||
else -> JSONObject(body?.string() ?: return null)
|
||||
}
|
||||
} finally {
|
||||
closeQuietly()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user