package com.adchain.exposdk import java.lang.reflect.Method /** * Telemetry bridge for the AdChain React Native layer. * * Forwards exceptions that the RN bridge *catches and swallows* onto the core SDK's * self-contained telemetry rail (`sdk_error`, category `sdk_diagnostic`) so bridge-side * failures land next to native ones — tagged `layer = "rn"`. * * The core hook (`com.adchain.sdk.telemetry.SdkTelemetry.reportSdkError`) is resolved via * **reflection** (lazy, once, cached). This keeps the RN bridge free of any compile-time * dependency on that symbol: older core pins that lack the method simply make every call a * permanent no-op. There is no third-party observability SDK here — this only relays into * first-party (`com.adchain.*`) code, per `OBSERVABILITY-ADR.md`. * * Fail-safe (highest priority): no path here may ever propagate a throwable to the caller. * Every call into [report] is fully guarded; a failure to relay is silently dropped. */ object AdchainRNTelemetry { /** Layer tag carried in the diagnostic event so RN-origin errors are distinguishable. */ private const val LAYER_RN = "rn" /** * Lazily-resolved, cached reflection handle to the core hook * `SdkTelemetry.reportSdkError(String, Throwable, String)` — `@JvmStatic`, so it is * invoked with a `null` receiver. Resolves to `null` (permanent no-op) when the core * class/method is absent on the current pin. */ private val reportMethod: Method? by lazy { resolveReportMethod() } private fun resolveReportMethod(): Method? { return try { Class.forName("com.adchain.sdk.telemetry.SdkTelemetry") .getMethod( "reportSdkError", String::class.java, Throwable::class.java, String::class.java ) } catch (t: Throwable) { // Class or method missing on this core pin → permanent no-op. null } } /** * Relay a caught bridge exception onto the core diagnostic rail with `layer = "rn"`. * * Network failures are excluded (they reflect the user's connectivity, not an SDK * malfunction). The core scrubs the throwable/message for PII, so call sites must not * pass PII in [message]. */ @JvmStatic fun report(message: String, throwable: Throwable?) { try { // Network failures are not SDK malfunctions — drop them (mirrors core). if (isNetworkThrowable(throwable)) return reportMethod?.invoke(null, message, throwable, LAYER_RN) } catch (t: Throwable) { // fail-safe: never let telemetry relay disturb the bridge. } } /** * True when the throwable (or any cause in its chain) is a [java.io.IOException] — a * network/transport failure rather than an SDK malfunction. Bounded, cycle-guarded * cause-chain walk (mirrors core `SdkTelemetry.isNetworkThrowable`). */ private fun isNetworkThrowable(throwable: Throwable?): Boolean { var current: Throwable? = throwable var depth = 0 val seen = java.util.Collections.newSetFromMap(java.util.IdentityHashMap()) while (current != null && depth < 16 && seen.add(current)) { if (current is java.io.IOException) return true current = try { current.cause } catch (t: Throwable) { null } depth++ } return false } }