Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fail State call twice and make error #680

Open
rzgonz opened this issue May 14, 2023 · 2 comments
Open

Fail State call twice and make error #680

rzgonz opened this issue May 14, 2023 · 2 comments

Comments

@rzgonz
Copy link

rzgonz commented May 14, 2023

i want changes error message Fail state whit this

suspend {
            userUseCase.getUserProfile()
        }.execute(Dispatchers.IO) {
            if(it is Fail){
                val errorMessage = handleErrorBE(it.error)
                copy(userProfileDto = Fail(errorMessage))
            }else{
                copy(userProfileDto = it)
            }

but this make an error

java.lang.IllegalArgumentException: Impure reducer set on Repository! userProfileDto changed from Fail(error=java.lang.Throwable: Internal Server Error, value=null) to Fail(error=java.lang.Throwable: HTTP 500 , value=null). Ensure that your state properties properly implement hashCode.

this error cause Fail state call twice when i see log output, then the last output message null

Screenshot 2023-05-14 at 7 55 56 AM

when i implement like this

            setState {
                copy(userProfileDto = Loading())
            }
            try {
                val response = userUseCase.getUserProfile()
                setState {
                    copy(userProfileDto = Success(response))
                }
            } catch (e: Exception) {
                val errorMessage = handleErrorBE(e)
                setState {
                    copy(userProfileDto = Fail(errorMessage))
                }
            }
        }

this works perfectly. when I use a function from Mavericks, it makes an error.

@gpeal
Copy link
Collaborator

gpeal commented May 14, 2023

It's impossible to help here without more context. Can you reproduce this in another project? What does handleErrorBE(it.error) do? Looking at the error message, something you're doing is causing errorMessage to change from java.lang.Throwable: Internal Server Error to java.lang.Throwable: HTTP 500 which makes the Mavericks impure check to correctly trigger.

@rzgonz
Copy link
Author

rzgonz commented May 24, 2023

@gpeal handleErrorBe is a function for Parce my api error message

fun handleErrorBE(error: Throwable): Throwable {
    val message = safeLaunchWithResult(error.message.orEmpty()) {
        val response = (error as? HttpException)?.response()?.errorBody()?.string()
        val gson = Gson().fromJson(response, ErrorResponse::class.java)
        gson.apiDisplayMessage.orEmpty()
    }
    return Throwable(message)
}
inline fun <reified T> safeLaunchWithResult(default: T, block: () -> T): T = try {
    block()
} catch (e: Exception) {
    e.printStackTrace()
    default
}

and return an error message to Fail Message for the state pemilihEditDtoAsyn

I use retrofit 2.9.0 for network connection and implement Kotlin delegation and jetpack compose

i try at same project with this code

 val requestBody: RequestBody = builder.build()
logD<PemilihEditViewModel>("state begin")
            suspend {
                pemilihUseCase.updatePemilih(state.pemilihDetailDto?.id.toString(), requestBody)
            }.execute(Dispatchers.IO) {
                logD<PemilihEditViewModel>("state $it")
                copy(pemilihEditDtoAsyn = it)
            }

and the output is

Screenshot 2023-05-25 at 9 10 56 AM

and this my repository
Screenshot 2023-05-25 at 9 11 42 AM

 @PUT("pemilih/{id}")
    suspen fun updatePemilih(
        @Path("id") id: String,
        @Body file: RequestBody
    ): BaseResponse<PemilihAddResponse>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants