Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StripeTerminalReactNativeModule reduced scope #202

Merged
merged 4 commits into from
Mar 24, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,5 @@ dependencies {
implementation 'com.stripe:stripeterminal:2.7.1'
implementation 'com.google.code.gson:gson:2.3.1'
implementation 'com.squareup.okhttp3:okhttp:4.9.1'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0'
}
28 changes: 23 additions & 5 deletions android/src/main/java/com/stripeterminalreactnative/Errors.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,46 @@ package com.stripeterminalreactnative

import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.bridge.WritableNativeMap
import com.stripe.stripeterminal.external.models.TerminalException
import com.stripe.stripeterminal.external.models.TerminalException.TerminalErrorCode
import kotlinx.coroutines.CancellationException
import kotlin.jvm.Throws

internal fun createError(exception: TerminalException): ReadableMap = nativeMapOf {
internal fun createError(throwable: Throwable): ReadableMap = nativeMapOf {
putMap("error", nativeMapOf {
putString("message", exception.errorMessage)
putString("code", exception.errorCode.toString())
writeError(throwable)
})
}

private fun WritableNativeMap.writeError(throwable: Throwable?) {
when (throwable) {
is TerminalException -> {
putString("message", throwable.errorMessage)
putString("code", throwable.errorCode.toString())
}
is CancellationException -> {
writeError(throwable.cause)
}
else -> {
putString("message", throwable?.message ?: "Unknown error")
putString("code", TerminalErrorCode.UNEXPECTED_SDK_ERROR.toString())
}
}
}

@Throws(TerminalException::class)
internal fun <T> requireCancelable(cancelable: T?, lazyMessage: () -> String): T {
return cancelable ?: throw TerminalException(
TerminalException.TerminalErrorCode.CANCEL_FAILED,
TerminalErrorCode.CANCEL_FAILED,
lazyMessage()
)
}

@Throws(TerminalException::class)
internal fun <T> requireParam(input: T?, lazyMessage: () -> String): T {
return input ?: throw TerminalException(
TerminalException.TerminalErrorCode.INVALID_REQUIRED_PARAMETER,
TerminalErrorCode.INVALID_REQUIRED_PARAMETER,
lazyMessage()
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.stripeterminalreactnative

import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.WritableNativeMap
import com.facebook.react.modules.core.DeviceEventManagerModule

internal object ReactExtensions {

fun ReactApplicationContext.sendEvent(
eventName: String,
resultBuilder: WritableNativeMap.() -> Unit
) {
getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
.emit(eventName, nativeMapOf {
resultBuilder()
})
}
}
Loading