title: How to create Generic Response handler for error and validate response using retrofit,rxjava and dagger
body: <p>I'm trying to create generic architecture to consume complex json structure as follows:</p>
  
  <blockquote>
    <p><strong>Json</strong> Format</p>
  </blockquote>
  
  <pre><code>{
  "type": "success",
  "code": "s-groups-0006",
  "description": "Index List successfully",
  "result": {
      "asOnDate": 1505457095278,
      "indexList": [
          {
              "change": "22.35",
              "changePercent": "0.27",
              "isDefault": true,
              "isEditable": false
          }
      ]
  }
  </code></pre>
  
  <p>}</p>
  
  <blockquote>
    <p><strong>Dagger</strong> Format</p>
  </blockquote>
  
  <pre><code>@Singleton
  fun provideGson(): Gson =
          GsonBuilder()
                  .setLenient()
               //   .registerTypeAdapter(BaseResponse::class.java, RestDeserializer&lt;BaseResponse&lt;T&gt;&gt;())
                  .create()
  </code></pre>
  
  <blockquote>
    <p><strong>Rest</strong> Deseralizers</p>
  </blockquote>
  
  <pre><code> class RestDeserializer&lt;T&gt; : JsonDeserializer&lt;T&gt; {
  @Throws(JsonParseException::class)
  override fun deserialize(je: JsonElement, type: Type, jdc: JsonDeserializationContext): T? {
      val content = je.asJsonObject
  
      // Deserialize it. You use a new instance of Gson to avoid infinite recursion
      // to this deserializer
      return Gson().fromJson&lt;T&gt;(content, type)
  
  }
  </code></pre>
  
  <p>}</p>
  
  <blockquote>
    <p><strong>Error callback</strong></p>
  </blockquote>
  
  <pre><code>abstract class ErrorCallBack&lt;T : BaseResponse&lt;T&gt;&gt; : DisposableObserver&lt;T&gt;() {
  
  protected abstract fun onSuccess(t: T)
  
  override fun onNext(t: T) {
      //You can return StatusCodes of different cases from your API and handle it here. I usually include these cases on BaseResponse and iherit it from every Response
      onSuccess(t)
  }
  
  override fun onError(e: Throwable) {
  
      when (e) {
          is HttpException -&gt; {
              val responseBody = (e).response().errorBody()
              responseBody?.let {
                  L.e("Error in call htttp exception")
              }
          }
          is SocketTimeoutException -&gt; {
              // todo
              L.e("Error in Socket time out")
          }
          is IOException -&gt; {
              // todo
              L.e("Error in IO Exception")
          }
          else -&gt; {
              e.message?.let {
                  // todo
              }
          }
      }
  }
  
  override fun onComplete() {
  
  }
  
  private fun getErrorMessage(responseBody: ResponseBody): String {
      return try {
          val jsonObject = JSONObject(responseBody.string())
          jsonObject.getString("message")
      } catch (e: Exception) {
          e.message!!
      }
  
  }
  </code></pre>
  
  <p>}</p>
  
  <blockquote>
    <p><strong>Repository</strong> </p>
  </blockquote>
  
  <pre><code>    override fun getValidateUser(validateUser: ValidateUser): LiveData&lt;ValidateUserResponse&gt; {
  
      val mutableLiveData = MutableLiveData&lt;ValidateUserResponse&gt;()
      remoteServices.requestValidateUser(validateUser)
              .subscribeOn(Schedulers.io())
              .observeOn(AndroidSchedulers.mainThread())
              .subscribeWith(object : ErrorCallBack&lt;BaseResponse&lt;ValidateUserResponse&gt;&gt;() {
                  override fun onSuccess(t: BaseResponse&lt;ValidateUserResponse&gt;) {
                      if (t.type == CommonContents.SUCCESS) {
                          L.d("Success in validate user")
                          mutableLiveData.value = transform(t)
                      } else {
                          L.e("Validate User Error")
                      }
                  }
              })
      return mutableLiveData
  }
  </code></pre>
  
  <blockquote>
    <p><strong>Data</strong> Class</p>
  </blockquote>
  
  <pre><code>data class BaseResponse&lt;out T&gt;(
      @SerializedName(CommonContents.TYPE) val type: String,
      @SerializedName(CommonContents.CODE) val Code: String,
      @SerializedName(CommonContents.DESCRIPTION) val Description: String,
      @SerializedName(CommonContents.RESULT)val result: T? = null)
  </code></pre>
  
  <p>These are my structures and I try to make a generic structure but am facing an issue when invoking the Error callback.</p>
  
  <p><strong>Please guide me how to achieve this. Can i call generic method inside generic Response?</strong></p>
  
  <pre><code>  .subscribeWith(object : ErrorCallBack&lt;BaseResponse&lt;ValidateUserResponse&gt;&gt;() {
  </code></pre>