2

Hi I have made ContextWrapper for retrofit error handling at single place and want to use it in subscribeWith.

abstract class CallbackWrapper<T : BaseResponse> : DisposableObserver<T>() {

    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 inherit it from every Response
        onSuccess(t)
    }

    override fun onError(e: Throwable) {
        when (e) {
            is HttpException -> {
                val responseBody = (e as HttpException).response().errorBody()
                responseBody?.let {
                    // view?.onUnknownError(getErrorMessage(it))
                }
            }
            is SocketTimeoutException -> {
                // view?.onTimeout()
            }
            is IOException -> {
                // view?.onNetworkError()
            }
            else -> {
                e.message?.let {
                    // view?.onUnknownError(it)
                }
            }
        }
    }


    override fun onComplete() {

    }

    private fun getErrorMessage(responseBody: ResponseBody): String {
        val jsonObject = JSONObject(responseBody.string())
        return jsonObject.getString(("message"))
    }
}

I am trying to use this like below

ApiHelperImpl().doServerLoginApiCall(email, password)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribeWith(CallbackWrapper<LoginResponse> {

                    })

enter image description here

I want to do something like https://blog.mindorks.com/rxjava2-and-retrofit2-error-handling-on-a-single-place-8daf720d42d6 Does anyone know how to do it ?

N Sharma
  • 33,489
  • 95
  • 256
  • 444

1 Answers1

4

You need to put the () after CallbackWrapper<LoginResponse> and to instantiate an abstract class in kotlin you have to call it as: object: AbstractClass(...params){}

ApiHelperImpl().doServerLoginApiCall(email, password)
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribeWith(object: CallbackWrapper<LoginResponse>() {
        override fun onSuccess(r: LoginResponse) { }
    })
pleft
  • 7,567
  • 2
  • 21
  • 45
  • If you see my `CallbackWrapper` I dont have view and Do we need new operator in Kotlin to create object ? – N Sharma Nov 06 '17 at 08:22
  • I didn't say anything about `view`, just about the `new` operator which appears in the example url you posted. – pleft Nov 06 '17 at 08:23
  • Yes, I think we don't need new operator to initialize in kotlin. Please correct me if I am wrong – N Sharma Nov 06 '17 at 08:24
  • Correct but you are also missing the `()` after `CallbackWrapper`. I will update my answer – pleft Nov 06 '17 at 08:26
  • see I am getting https://imgur.com/a/jg6Ab can not create an instance of an abstract class – N Sharma Nov 06 '17 at 08:28
  • https://stackoverflow.com/questions/34143564/create-an-instance-of-an-abstract-class-in-kotlin – pleft Nov 06 '17 at 08:30
  • You need to implement the `onSuccess` abstract method – pleft Nov 06 '17 at 08:39
  • 2
    Thank you so much :) I didn't know how to instantiate abstract class +1 – N Sharma Nov 06 '17 at 08:41
  • Does anyone know in Kotlin if there is a nice shorter way to override onSuccess using `it` and lambda arrows `override fun onSuccess(r: LoginResponse) { }` ? I can't do it :) – Daniel Wilson Nov 06 '17 at 19:19