Skip to content

Commit b6eb309

Browse files
authored
Updating Error Source Pointer Logic (#244)
* updating error pointer logic * fix test
1 parent 69254e0 commit b6eb309

File tree

3 files changed

+70
-5
lines changed

3 files changed

+70
-5
lines changed

sdk/analytics/src/main/java/com/klaviyo/analytics/networking/requests/KlaviyoErrorResponse.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ data class KlaviyoErrorSource(
3333
) {
3434
internal companion object {
3535
// current path objects from the backend
36-
const val EMAIL_PATH = "/data/attributes/email"
37-
const val PHONE_NUMBER_PATH = "/data/attributes/phone_number"
36+
const val EMAIL_PATH = "attributes/email"
37+
const val PHONE_NUMBER_PATH = "attributes/phone_number"
3838
}
3939
}

sdk/analytics/src/main/java/com/klaviyo/analytics/state/StateSideEffects.kt

+4-3
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,17 @@ internal class StateSideEffects(
117117
request.responseCode == HTTP_BAD_REQUEST -> {
118118
request.errorBody.errors.find { it.title == KlaviyoErrorResponse.INVALID_INPUT_TITLE }
119119
?.let { inputError ->
120-
when (inputError.source?.pointer) {
121-
KlaviyoErrorSource.EMAIL_PATH -> {
120+
val pointer = inputError.source?.pointer
121+
when {
122+
pointer?.contains(KlaviyoErrorSource.EMAIL_PATH) == true -> {
122123
(Registry.get<State>() as? KlaviyoState)?.resetEmail().also {
123124
Registry.log.warning(
124125
"Invalid email - resetting email state to null"
125126
)
126127
}
127128
}
128129

129-
KlaviyoErrorSource.PHONE_NUMBER_PATH -> {
130+
pointer?.contains(KlaviyoErrorSource.PHONE_NUMBER_PATH) == true -> {
130131
(Registry.get<State>() as? KlaviyoState)?.resetPhoneNumber().also {
131132
Registry.log.warning(
132133
"Invalid phone number - resetting phone number state to null"

sdk/analytics/src/test/java/com/klaviyo/analytics/state/StateSideEffectsTest.kt

+64
Original file line numberDiff line numberDiff line change
@@ -340,4 +340,68 @@ class StateSideEffectsTest : BaseTest() {
340340

341341
assertFalse(capturedPushState.isCaptured)
342342
}
343+
344+
@Test
345+
fun `updated phone error source pointer still resets state`() {
346+
Registry.register<State>(klaviyoStateMock)
347+
StateSideEffects(
348+
state = klaviyoStateMock,
349+
apiClient = apiClientMock
350+
)
351+
352+
capturedApiObserver.captured(
353+
mockk<ProfileApiRequest>().apply {
354+
every { status } returns KlaviyoApiRequest.Status.Failed
355+
every { responseCode } returns 400
356+
every { errorBody } returns KlaviyoErrorResponse(
357+
listOf(
358+
KlaviyoError(
359+
id = "67ed6dbf-1653-499b-a11d-30310aa01ff7",
360+
status = 400,
361+
title = "Invalid input.",
362+
detail = "Invalid phone number format (Example of a valid format: +12345678901)",
363+
source = KlaviyoErrorSource(
364+
pointer = "/data/attributes/profile/data/attributes/phone_number"
365+
)
366+
)
367+
)
368+
)
369+
}
370+
)
371+
372+
verify { klaviyoStateMock.resetPhoneNumber() }
373+
Registry.unregister<State>()
374+
}
375+
376+
@Test
377+
fun `updated email error source pointer still resets state`() {
378+
Registry.register<State>(klaviyoStateMock)
379+
StateSideEffects(
380+
state = klaviyoStateMock,
381+
apiClient = apiClientMock
382+
)
383+
384+
capturedApiObserver.captured(
385+
mockk<ProfileApiRequest>().apply {
386+
every { status } returns KlaviyoApiRequest.Status.Failed
387+
every { responseCode } returns 400
388+
every { errorBody } returns KlaviyoErrorResponse(
389+
listOf(
390+
KlaviyoError(
391+
id = "67ed6dbf-1653-499b-a11d-30310aa01ff7",
392+
status = 400,
393+
title = "Invalid input.",
394+
detail = "This email is complete chicanery",
395+
source = KlaviyoErrorSource(
396+
pointer = "/data/attributes/profile/data/attributes/email"
397+
)
398+
)
399+
)
400+
)
401+
}
402+
)
403+
404+
verify { klaviyoStateMock.resetEmail() }
405+
Registry.unregister<State>()
406+
}
343407
}

0 commit comments

Comments
 (0)