-
-
Notifications
You must be signed in to change notification settings - Fork 342
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
318 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...rc/test/java/io/sentry/rnsentryandroidtester/RNSentryCompositeOptionsConfigurationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package io.sentry.rnsentryandroidtester | ||
|
||
import io.sentry.Sentry.OptionsConfiguration | ||
import io.sentry.android.core.SentryAndroidOptions | ||
import io.sentry.react.RNSentryCompositeOptionsConfiguration | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.junit.runners.JUnit4 | ||
import org.mockito.kotlin.mock | ||
import org.mockito.kotlin.verify | ||
|
||
@RunWith(JUnit4::class) | ||
class RNSentryCompositeOptionsConfigurationTest { | ||
@Test | ||
fun `configure should call base and overriding configurations`() { | ||
val baseConfig: OptionsConfiguration<SentryAndroidOptions> = mock() | ||
val overridingConfig: OptionsConfiguration<SentryAndroidOptions> = mock() | ||
|
||
val compositeConfig = RNSentryCompositeOptionsConfiguration(baseConfig, overridingConfig) | ||
val options = SentryAndroidOptions() | ||
compositeConfig.configure(options) | ||
|
||
verify(baseConfig).configure(options) | ||
verify(overridingConfig).configure(options) | ||
} | ||
|
||
@Test | ||
fun `configure should apply base configuration and override values`() { | ||
val baseConfig = | ||
OptionsConfiguration<SentryAndroidOptions> { options -> | ||
options.dsn = "https://[email protected]" | ||
options.isDebug = false | ||
options.release = "some-release" | ||
} | ||
val overridingConfig = | ||
OptionsConfiguration<SentryAndroidOptions> { options -> | ||
options.dsn = "https://[email protected]" | ||
options.isDebug = true | ||
options.environment = "production" | ||
} | ||
|
||
val compositeConfig = RNSentryCompositeOptionsConfiguration(baseConfig, overridingConfig) | ||
val options = SentryAndroidOptions() | ||
compositeConfig.configure(options) | ||
|
||
assert(options.dsn == "https://[email protected]") // overridden value | ||
assert(options.isDebug) // overridden value | ||
assert(options.release == "some-release") // base value not overridden | ||
assert(options.environment == "production") // overridden value not in base | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...ges/core/android/src/main/java/io/sentry/react/RNSentryCompositeOptionsConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package io.sentry.react; | ||
|
||
import io.sentry.Sentry.OptionsConfiguration; | ||
import io.sentry.android.core.SentryAndroidOptions; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class RNSentryCompositeOptionsConfiguration | ||
implements OptionsConfiguration<SentryAndroidOptions> { | ||
private final OptionsConfiguration<SentryAndroidOptions> baseConfiguration; | ||
private final OptionsConfiguration<SentryAndroidOptions> overridingConfiguration; | ||
|
||
public RNSentryCompositeOptionsConfiguration( | ||
OptionsConfiguration<SentryAndroidOptions> baseConfiguration, | ||
OptionsConfiguration<SentryAndroidOptions> overridingConfiguration) { | ||
this.baseConfiguration = baseConfiguration; | ||
this.overridingConfiguration = overridingConfiguration; | ||
} | ||
|
||
@Override | ||
public void configure(@NotNull SentryAndroidOptions options) { | ||
baseConfiguration.configure(options); | ||
overridingConfiguration.configure(options); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
packages/core/android/src/main/java/io/sentry/react/RNSentrySDK.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package io.sentry.react; | ||
|
||
import android.content.Context; | ||
import com.facebook.react.bridge.ReadableMap; | ||
import io.sentry.ILogger; | ||
import io.sentry.Sentry; | ||
import io.sentry.SentryLevel; | ||
import io.sentry.android.core.AndroidLogger; | ||
import io.sentry.android.core.SentryAndroidOptions; | ||
import java.io.BufferedReader; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.json.JSONObject; | ||
|
||
public final class RNSentrySDK { | ||
private static final String CONFIGURATION_FILE = "sentry.options.json"; | ||
private static final String NAME = "RNSentrySDK"; | ||
|
||
private static final ILogger logger = new AndroidLogger(NAME); | ||
|
||
private RNSentrySDK() { | ||
throw new AssertionError("Utility class should not be instantiated"); | ||
} | ||
|
||
/** | ||
* Start the Native Android SDK with the provided options | ||
* | ||
* @param context Android Context | ||
* @param configuration configuration options | ||
*/ | ||
public static void init( | ||
@NotNull final Context context, | ||
@NotNull Sentry.OptionsConfiguration<SentryAndroidOptions> configuration) { | ||
try { | ||
JSONObject jsonObject = getOptionsFromConfigurationFile(context); | ||
ReadableMap rnOptions = RNSentryMapConverter.jsonObjectToReadableMap(jsonObject); | ||
RNSentryStart.startWithOptions(context, rnOptions, configuration, null, logger); | ||
} catch (Exception e) { | ||
logger.log( | ||
SentryLevel.ERROR, "Failed to start Sentry with options from configuration file.", e); | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
/** | ||
* Start the Native Android SDK with options from `sentry.options.json` configuration file | ||
* | ||
* @param context Android Context | ||
*/ | ||
public static void init(@NotNull final Context context) { | ||
init(context, options -> {}); | ||
} | ||
|
||
private static JSONObject getOptionsFromConfigurationFile(Context context) { | ||
try (InputStream inputStream = context.getAssets().open(CONFIGURATION_FILE); | ||
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { | ||
|
||
StringBuilder stringBuilder = new StringBuilder(); | ||
String line; | ||
while ((line = reader.readLine()) != null) { | ||
stringBuilder.append(line); | ||
} | ||
String configFileContent = stringBuilder.toString(); | ||
return new JSONObject(configFileContent); | ||
|
||
} catch (Exception e) { | ||
logger.log( | ||
SentryLevel.ERROR, | ||
"Failed to read configuration file. Please make sure " | ||
+ CONFIGURATION_FILE | ||
+ " exists in the root of your project.", | ||
e); | ||
return null; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ import com.facebook.soloader.SoLoader | |
import io.sentry.Hint | ||
import io.sentry.SentryEvent | ||
import io.sentry.SentryOptions.BeforeSendCallback | ||
import io.sentry.android.core.SentryAndroid | ||
import io.sentry.react.RNSentrySDK | ||
|
||
class MainApplication : | ||
Application(), | ||
|
@@ -51,9 +51,8 @@ class MainApplication : | |
} | ||
|
||
private fun initializeSentry() { | ||
SentryAndroid.init(this) { options -> | ||
// Only options set here will apply to the Android SDK | ||
// Options from JS are not passed to the Android SDK when initialized manually | ||
RNSentrySDK.init(this) { options -> | ||
// Options set here will apply to the Android SDK overriding the ones from `sentry.options.json` | ||
options.dsn = "https://[email protected]/5428561" | ||
options.isDebug = true | ||
|
||
|
@@ -74,5 +73,7 @@ class MainApplication : | |
event | ||
} | ||
} | ||
|
||
// RNSentrySDK.init(this) | ||
} | ||
} |
Oops, something went wrong.