Skip to content

Commit 3145e04

Browse files
committed
feat(flow): parse bridge errors into typed DescopeException
descope/etc#16441
1 parent 9cc90ed commit 3145e04

6 files changed

Lines changed: 40 additions & 18 deletions

File tree

descopesdk/src/main/java/com/descope/android/flow/DescopeFlowBridge.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import com.descope.internal.http.REFRESH_COOKIE_NAME
1515
import com.descope.internal.others.error
1616
import com.descope.internal.others.info
1717
import com.descope.internal.others.stringOrEmptyAsNull
18+
import com.descope.internal.others.toDescopeException
1819
import com.descope.internal.others.with
1920
import com.descope.sdk.DescopeLogger
2021
import com.descope.types.DescopeException
@@ -100,7 +101,7 @@ internal class FlowBridge(webView: WebView) {
100101

101102
private fun bridgeOnError(error: String) {
102103
handler.post {
103-
listener?.onError(DescopeException.flowFailed.with(message = error))
104+
listener?.onError(error.toDescopeException() ?: DescopeException.flowFailed.with(message = error))
104105
}
105106
}
106107

descopesdk/src/main/java/com/descope/internal/http/ClientErrors.kt

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,6 @@ package com.descope.internal.http
33
import com.descope.internal.others.toMap
44
import com.descope.internal.others.with
55
import com.descope.types.DescopeException
6-
import org.json.JSONObject
7-
8-
internal fun parseServerError(response: String): DescopeException? {
9-
try {
10-
val map = JSONObject(response).toMap()
11-
val code = map["errorCode"] as? String ?: return null
12-
val desc = map["errorDescription"] as? String ?: "Descope server error"
13-
val message = map["errorMessage"] as? String
14-
return DescopeException(code = code, desc = desc, message = message)
15-
} catch (_: Exception) {
16-
return null
17-
}
18-
}
196

207
internal fun exceptionFromResponseCode(code: Int): DescopeException? {
218
val desc = failureFromResponseCode(code) ?: return null

descopesdk/src/main/java/com/descope/internal/http/DescopeClient.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.descope.internal.http
22

33
import com.descope.android.SystemInfo
4+
import com.descope.internal.others.toDescopeException
45
import com.descope.sdk.DescopeConfig
56
import com.descope.sdk.DescopeSdk
67
import com.descope.types.DeliveryMethod
@@ -505,7 +506,7 @@ internal open class DescopeClient(internal val config: DescopeConfig, systemInfo
505506

506507
override val defaultHeaders: Map<String, String> = makeDefaultHeaders(config, systemInfo)
507508

508-
override fun exceptionFromResponse(response: String): DescopeException? = parseServerError(response)
509+
override fun exceptionFromResponse(response: String): DescopeException? = response.toDescopeException()
509510

510511
// Internal
511512

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
package com.descope.internal.others
22

33
import com.descope.types.DescopeException
4+
import org.json.JSONObject
45

56
internal fun DescopeException.with(desc: String? = null, message: String? = null, cause: Throwable? = null) = DescopeException(
67
code = code,
78
desc = desc ?: this.desc,
89
message = message ?: this.message,
910
cause = cause ?: this.cause,
1011
)
12+
13+
internal fun String.toDescopeException(): DescopeException? {
14+
try {
15+
val map = JSONObject(this).toMap()
16+
val code = map["errorCode"] as? String ?: return null
17+
val desc = map["errorDescription"] as? String ?: "Descope server error"
18+
val message = map["errorMessage"] as? String
19+
return DescopeException(code = code, desc = desc, message = message)
20+
} catch (_: Exception) {
21+
return null
22+
}
23+
}

descopesdk/src/main/java/com/descope/types/Error.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ class DescopeException(
108108
val flowFailed = DescopeException(code = "K100001", desc = "Flow failed to run")
109109
val flowCancelled = DescopeException(code = "K100002", desc = "Flow cancelled")
110110
val flowSetup = DescopeException(code = "K100003", desc = "Flow not properly set up")
111+
val flowAborted = DescopeException(code = "E102122", desc = "Flow aborted")
111112

112113
val widgetFailed = DescopeException(code = "K100101", desc = "Widget failed to run")
113114
val widgetAuthenticationRequired = DescopeException(code = "K100102", desc = "Widget requires an authenticated session")

descopesdk/src/test/java/com/descope/types/DescopeExceptionTest.kt

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package com.descope.types
22

3-
import com.descope.internal.http.parseServerError
3+
import com.descope.internal.others.toDescopeException
44
import org.json.JSONObject
55
import org.junit.Assert.assertEquals
6+
import org.junit.Assert.assertNull
67
import org.junit.Assert.fail
78
import org.junit.Test
89

@@ -15,12 +16,30 @@ class DescopeExceptionTest {
1516
put("errorDescription", "server description")
1617
put("errorMessage", "some reason")
1718
}.toString()
18-
val serverException = parseServerError(mockResponse)
19+
val serverException = mockResponse.toDescopeException()
1920
assertEquals(DescopeException.wrongOtpCode, serverException)
2021
when (serverException) {
2122
DescopeException.wrongOtpCode -> assertEquals("server description", serverException.desc)
2223
else -> fail("wrong when clause")
2324
}
2425
}
25-
26+
27+
@Test
28+
fun flow_aborted_parsing() {
29+
val payload = JSONObject().apply {
30+
put("errorCode", "E102122")
31+
put("errorDescription", "Flow aborted")
32+
put("errorMessage", "User canceled")
33+
}.toString()
34+
val exception = payload.toDescopeException()
35+
assertEquals(DescopeException.flowAborted, exception)
36+
assertEquals("User canceled", exception?.message)
37+
}
38+
39+
@Test
40+
fun invalid_error_payload_parsing() {
41+
assertNull("not a json error".toDescopeException())
42+
assertNull(JSONObject().apply { put("errorMessage", "no code here") }.toString().toDescopeException())
43+
}
44+
2645
}

0 commit comments

Comments
 (0)