Skip to content

Commit

Permalink
Make PurchasesError java.io.Serializable
Browse files Browse the repository at this point in the history
  • Loading branch information
tonidero committed Jan 8, 2025
1 parent eb3c8f4 commit e4bb7c0
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.revenuecat.purchases

import android.os.Bundle
import android.os.Parcel
import androidx.test.ext.junit.runners.AndroidJUnit4
import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class ErrorsTest {

@Test
fun errorIsSerializable() {
val purchasesError = PurchasesError(
PurchasesErrorCode.ConfigurationError,
"Underlying error message",
)
val purchasesException = PurchasesException(purchasesError)
val bundle = Bundle().apply {
putSerializable("exception", purchasesException)
}
val parcel = Parcel.obtain()
val readBundle: Bundle?
try {
parcel.writeBundle(bundle)
parcel.setDataPosition(0)
readBundle = parcel.readBundle(javaClass.classLoader)!!
} finally {
parcel.recycle()
}
assertThat(readBundle).isNotNull
val serializedException = readBundle!!.getSerializable("exception", PurchasesException::class.java)
assertThat(serializedException!!.code).isEqualTo(purchasesException.code)
assertThat(serializedException.underlyingErrorMessage).isEqualTo(purchasesException.underlyingErrorMessage)
}
}
8 changes: 7 additions & 1 deletion purchases/src/main/kotlin/com/revenuecat/purchases/errors.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.revenuecat.purchases
import android.os.Parcelable
import kotlinx.parcelize.IgnoredOnParcel
import kotlinx.parcelize.Parcelize
import java.io.Serializable

typealias PurchasesErrorCallback = (PurchasesError) -> Unit

Expand All @@ -16,7 +17,12 @@ typealias PurchasesErrorCallback = (PurchasesError) -> Unit
class PurchasesError(
val code: PurchasesErrorCode,
val underlyingErrorMessage: String? = null,
) : Parcelable {
) : Parcelable, Serializable {

companion object {
private const val serialVersionUID = 81719171L
}

// Message explaining the error
@IgnoredOnParcel
val message: String = code.description
Expand Down

0 comments on commit e4bb7c0

Please sign in to comment.