Customize onError() Message in Rx java from server API error output

Viewed 33

I wanted to print the error which API message send, Like there are many kinds of them example: Email already present, Username already exits etc. But this print only HTTP 400 Bad Request

fun doSignUp(
        firstName: String,
        lastName: String,
        email: String,
        password: String,
        deviceId: String
    ) {

        ApiHelper().userSignup(
            firstName,
            lastName,
            email,
            password,
            "Android",
            strAdd)
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : Observer<UserResponse>{
                override fun onComplete() {
                    Log.d("kb", "onComplete Call")
                }

                override fun onSubscribe(d: Disposable) {
                    Log.d("kb", "onSubscribe")
                }

                override fun onNext(t: UserResponse) {
                    Toast.makeText(applicationContext,"Please Upload Profile Pic...", Toast.LENGTH_SHORT).show()
                    val intent = Intent(applicationContext, SignupStep3Activity::class.java)
                    mPref.setAccessToken(t.data.accessToken)
                    startActivity(intent)
                }


                override fun onError(e: Throwable) {
                   Toast.makeText(applicationContext, e.localizedMessage, Toast.LENGTH_LONG).show()
                }
            })
    }

Here is what my api show on error response

    "message": "Email Already Exists",
    "code": 400,
    "errors": {
        "error_id": "15",
        "error_text": "Email Already Exists"
    },
    "data": []
}

I'm new to kotlin and here, pardon me for any mistake

1 Answers

In your onNext() you need something like this :

when (UserResponse.code()) {
                        200 -> {//Send appropriate message with intent here}
                        400 -> {//Send appropriate message with intent here}
                        401 -> {//Send appropriate message with intent here}
                        712 -> {//Send appropriate message with intent here}
}
Related