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.removeJSONObject(name: String) = remove(name) as JSONObject
|
||||||
|
|
||||||
private fun JSONObject.removeJSONArray(name: String) = remove(name) as JSONArray
|
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.MediaType.Companion.toMediaType
|
||||||
import okhttp3.RequestBody.Companion.toRequestBody
|
import okhttp3.RequestBody.Companion.toRequestBody
|
||||||
import okhttp3.Response
|
import okhttp3.Response
|
||||||
|
import okhttp3.internal.closeQuietly
|
||||||
|
import okio.IOException
|
||||||
import org.json.JSONObject
|
import org.json.JSONObject
|
||||||
import org.koitharu.kotatsu.parsers.util.parseJson
|
|
||||||
import java.net.HttpURLConnection
|
import java.net.HttpURLConnection
|
||||||
|
|
||||||
private val TYPE_JSON = "application/json".toMediaType()
|
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 JSONObject.toRequestBody() = toString().toRequestBody(TYPE_JSON)
|
||||||
|
|
||||||
fun Response.parseJsonOrNull(): JSONObject? {
|
fun Response.parseJsonOrNull(): JSONObject? {
|
||||||
return if (code == HttpURLConnection.HTTP_NO_CONTENT) {
|
return try {
|
||||||
null
|
when {
|
||||||
} else {
|
!isSuccessful -> throw IOException(body?.string())
|
||||||
parseJson()
|
code == HttpURLConnection.HTTP_NO_CONTENT -> null
|
||||||
|
else -> JSONObject(body?.string() ?: return null)
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
closeQuietly()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user