package com.blaze.rtnblazesdk.utils import androidx.annotation.Keep import com.blaze.blazesdk.shared.results.BlazeResult import com.blaze.rtnblazesdk.utils.parsing.toJsonString import com.facebook.react.bridge.Arguments import com.facebook.react.bridge.Promise import com.facebook.react.bridge.WritableMap // Represents error code that can be converted into a BlazeError on the React Native side. const val blazeConvertibleErrorCode = "BLAZE_CONVERTIBLE_ERROR" // Represents a generic error code on the Blaze React Native side. const val blazeReactNativeErrorCode = "BLAZE_REACT_NATIVE_ERROR" @Keep fun Promise.handleResult(result: BlazeResult) { when (result) { is BlazeResult.Success -> { if (result.value is Unit) { resolve(null) // Or resolve with any other appropriate value } else { resolve(result.value) } } is BlazeResult.Error -> { val reactNativeErrorString = result.toReactValue() val userInfo: WritableMap = Arguments.createMap().apply { putString("details", reactNativeErrorString) } reject( code = blazeConvertibleErrorCode, message = result.message, userInfo = userInfo ) } } } @Keep fun Promise.rejectWith(message: String) { reject(blazeReactNativeErrorCode, message) } @Keep data class BlazeReactNativeError( val domain: String?, val reason: String?, val message: String?, val metadata: Map?, val underlyingError: String?, ) { companion object { fun fromBlazeError(error: BlazeResult.Error): BlazeReactNativeError { return BlazeReactNativeError( domain = error.domain?.name, reason = error.reason?.name, message = error.message, metadata = error.metadata, underlyingError = error.cause?.message ) } } } /** * Extension function for BlazeResult.Error to convert to React Native value Uses the existing * BlazeReactNativeError serialization pattern */ fun BlazeResult.Error.toReactValue(): String? { val reactNativeError = BlazeReactNativeError.fromBlazeError(this) return reactNativeError.toJsonString() }