1
0
mirror of https://github.com/etesync/android synced 2025-01-22 13:40:55 +00:00

Gracefully handle getting non json responses when expecting json.

This commit is contained in:
Tom Hacohen 2019-02-13 18:52:00 +00:00
parent 143092ecf3
commit ea0add8f45

View File

@ -24,7 +24,11 @@ abstract class BaseManager {
}
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()) {
HttpURLConnection.HTTP_BAD_GATEWAY -> throw Exceptions.BadGatewayException(response, "Bad gateway: most likely a server restart")
@ -46,10 +50,10 @@ abstract class BaseManager {
return response
}
internal class ApiError {
var detail: String? = null
internal class ApiError(
var detail: String? = null,
var code: String? = null
}
)
open class Base {
var content: ByteArray? = null