mirror of
https://github.com/etesync/android
synced 2025-07-08 15:48:17 +00:00
Gracefully handle getting non json responses when expecting json.
This commit is contained in:
parent
143092ecf3
commit
ea0add8f45
@ -24,7 +24,11 @@ abstract class BaseManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!response.isSuccessful) {
|
if (!response.isSuccessful) {
|
||||||
val apiError = GsonHelper.gson.fromJson(response.body()!!.charStream(), ApiError::class.java)
|
val apiError = if (response.header("Content-Type", "application/json")!!.startsWith("application/json"))
|
||||||
|
GsonHelper.gson.fromJson(response.body()!!.charStream(), ApiError::class.java)
|
||||||
|
else
|
||||||
|
ApiError(code="got_html", detail="Got HTML while expecting JSON")
|
||||||
|
|
||||||
|
|
||||||
when (response.code()) {
|
when (response.code()) {
|
||||||
HttpURLConnection.HTTP_BAD_GATEWAY -> throw Exceptions.BadGatewayException(response, "Bad gateway: most likely a server restart")
|
HttpURLConnection.HTTP_BAD_GATEWAY -> throw Exceptions.BadGatewayException(response, "Bad gateway: most likely a server restart")
|
||||||
@ -46,10 +50,10 @@ abstract class BaseManager {
|
|||||||
return response
|
return response
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class ApiError {
|
internal class ApiError(
|
||||||
var detail: String? = null
|
var detail: String? = null,
|
||||||
var code: String? = null
|
var code: String? = null
|
||||||
}
|
)
|
||||||
|
|
||||||
open class Base {
|
open class Base {
|
||||||
var content: ByteArray? = null
|
var content: ByteArray? = null
|
||||||
|
Loading…
Reference in New Issue
Block a user