Commit 19977c81 authored by Vladislav Bogdashkin's avatar Vladislav Bogdashkin 🎣

added custom api error resolver

parent 5f55c936
...@@ -3,10 +3,7 @@ package com.biganto.visual.roompark.data.repository.api.retrofit.di ...@@ -3,10 +3,7 @@ package com.biganto.visual.roompark.data.repository.api.retrofit.di
import android.content.Context import android.content.Context
import com.biganto.visual.roompark.data.repository.api.retrofit.IRoomParkMobileApi import com.biganto.visual.roompark.data.repository.api.retrofit.IRoomParkMobileApi
import com.biganto.visual.roompark.data.repository.api.retrofit.raw.ErrorRaw import com.biganto.visual.roompark.data.repository.api.retrofit.raw.ErrorRaw
import com.biganto.visual.roompark.data.repository.api.retrofit.util.DateSerializer import com.biganto.visual.roompark.data.repository.api.retrofit.util.*
import com.biganto.visual.roompark.data.repository.api.retrofit.util.ErrorsListDeserializer
import com.biganto.visual.roompark.data.repository.api.retrofit.util.NullOnEmptyConverterFactory
import com.biganto.visual.roompark.data.repository.api.retrofit.util.VersionValidationInterceptor
import com.biganto.visual.roompark.data.service.network.INetworkMonitor import com.biganto.visual.roompark.data.service.network.INetworkMonitor
import com.biganto.visual.roompark.data.service.network.LiveNetworkMonitor import com.biganto.visual.roompark.data.service.network.LiveNetworkMonitor
import com.biganto.visual.roompark.data.service.network.NoNetworkException import com.biganto.visual.roompark.data.service.network.NoNetworkException
...@@ -46,6 +43,7 @@ class RetrofitModule{ ...@@ -46,6 +43,7 @@ class RetrofitModule{
private fun gsonConverterFactory() = GsonConverterFactory.create( private fun gsonConverterFactory() = GsonConverterFactory.create(
GsonBuilder() GsonBuilder()
.registerTypeHierarchyAdapter(Any::class.java,CustomExceptionDeserializer<Any>())
.registerTypeAdapter(genericType<MutableList<ErrorRaw>>(), ErrorsListDeserializer()) .registerTypeAdapter(genericType<MutableList<ErrorRaw>>(), ErrorsListDeserializer())
.registerTypeAdapter(Date::class.java, DateSerializer()) .registerTypeAdapter(Date::class.java, DateSerializer())
.setLenient() .setLenient()
......
package com.biganto.visual.roompark.data.repository.api.retrofit.util
import com.biganto.visual.roompark.data.repository.api.retrofit.raw.ErrorRaw
import com.biganto.visual.roompark.domain.custom_exception.parseException
import com.google.gson.Gson
import com.google.gson.JsonDeserializationContext
import com.google.gson.JsonDeserializer
import com.google.gson.JsonElement
import org.json.JSONArray
import org.json.JSONObject
import timber.log.Timber
import java.lang.reflect.Type
/**
* Created by Vladislav Bogdashkin on 12.11.2019.
*/
class CustomExceptionDeserializer<T> : JsonDeserializer<T> {
override fun deserialize(json: JsonElement, typeOfT: Type, ctx: JsonDeserializationContext): T {
val vals = ArrayList<ErrorRaw>()
//region valid case
when(json){
is JSONObject -> if (!json.asJsonObject.keySet().contains("errors"))
return Gson().newBuilder().create().fromJson(json,typeOfT)
is JSONArray -> return Gson().newBuilder().create().fromJson(json,typeOfT)
}
//endregion valid case
//region parse errorList
val errorList = json.asJsonObject.get("errors")
Timber.w("api errorlist: $errorList")
when(json) {
is JSONArray -> errorList.asJsonArray.forEach{
vals.add(ctx.deserialize<T>(it, ErrorRaw::class.java) as ErrorRaw)
}
is JSONObject -> json.asJsonObject.get("errors").asJsonArray.forEach {
vals.add(ctx.deserialize<T>(it, ErrorRaw::class.java) as ErrorRaw)
}
else -> throw RuntimeException("Unexpected JSON type: " + json.javaClass)
}
if (vals.size==0)
error("unhandled api result : $json")
throw parseException(vals.first()) //-> for now throws only first!
//endregion
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment