diff --git a/CHANGELOG.md b/CHANGELOG.md index ab867b14d..d44457a2e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +**Breaking Changes**: + +- Android NDK: `SentryNdk.init(NdkOptions)` now throws an `Exception` if init fails (non-zero return code) rather than silently swallowing the error. ([#1430](https://github.com/getsentry/sentry-native/pull/1430)) + **Fixes**: - Removed the 10-item limit per envelope for non-session data. Sessions are now limited to 100 per envelope, while other items (e.g., attachments) have no limit in amount. ([#1347](https://github.com/getsentry/sentry-native/pull/1347)) diff --git a/ndk/lib/src/androidTest/java/io/sentry/ndk/SentryNdkTest.java b/ndk/lib/src/androidTest/java/io/sentry/ndk/SentryNdkTest.java index 76f4ca0fa..ad9907740 100644 --- a/ndk/lib/src/androidTest/java/io/sentry/ndk/SentryNdkTest.java +++ b/ndk/lib/src/androidTest/java/io/sentry/ndk/SentryNdkTest.java @@ -65,6 +65,30 @@ public void shutdownDoesNotFail() throws IOException { // it does not crash } + @Test(expected = IllegalStateException.class) + public void initThrowsException() throws IOException { + final TemporaryFolder temporaryFolder = TemporaryFolder.builder().build(); + temporaryFolder.create(); + final File outboxPath = temporaryFolder.newFolder("outboxPath"); + + //noinspection DataFlowIssue + final NdkOptions options = + new NdkOptions( + null, + true, + outboxPath.getAbsolutePath(), + "1.0.0", + "production", + "dist", + 100, + "io.sentry.ndk"); + + // when initialized with a NULL dsn + SentryNdk.init(options); + + // then it does crash + } + @Test public void messageCaught() throws IOException { final TemporaryFolder temporaryFolder = TemporaryFolder.builder().build(); diff --git a/ndk/lib/src/main/java/io/sentry/ndk/SentryNdk.java b/ndk/lib/src/main/java/io/sentry/ndk/SentryNdk.java index d43ac268a..72046377f 100644 --- a/ndk/lib/src/main/java/io/sentry/ndk/SentryNdk.java +++ b/ndk/lib/src/main/java/io/sentry/ndk/SentryNdk.java @@ -10,7 +10,13 @@ public final class SentryNdk { private SentryNdk() {} - private static native void initSentryNative(@NotNull final NdkOptions options); + /** + * Initializes sentry-native and returns 0 on success, non-zero on failure. + * + * @return -1 if an JNI or options configuration issue occurred, 1 if sentry native itself failed + * to initialize + */ + private static native int initSentryNative(@NotNull final NdkOptions options); private static native void shutdown(); @@ -18,10 +24,17 @@ private SentryNdk() {} * Init the NDK integration * * @param options the SentryAndroidOptions + * @throws IllegalStateException if sentry-native couldn't be initialized */ public static void init(@NotNull final NdkOptions options) { loadNativeLibraries(); - initSentryNative(options); + final int returnCode = initSentryNative(options); + if (returnCode > 0) { + throw new IllegalStateException( + "A sentry-native internal init error occurred, please check the logs for more details."); + } else if (returnCode < 0) { + throw new IllegalStateException("A sentry-native setup failure occurred"); + } } /** Closes the NDK integration */ diff --git a/ndk/lib/src/main/jni/sentry.c b/ndk/lib/src/main/jni/sentry.c index 827ec5bab..6be3765c3 100644 --- a/ndk/lib/src/main/jni/sentry.c +++ b/ndk/lib/src/main/jni/sentry.c @@ -247,7 +247,7 @@ static void send_envelope(sentry_envelope_t *envelope, void *data) { sentry_envelope_free(envelope); } -JNIEXPORT void JNICALL +JNIEXPORT jint JNICALL Java_io_sentry_ndk_SentryNdk_initSentryNative( JNIEnv *env, jclass cls, @@ -355,8 +355,8 @@ Java_io_sentry_ndk_SentryNdk_initSentryNative( jfloat traces_sample_rate = (jfloat) (*env)->CallFloatMethod(env, sentry_ndk_options, traces_sample_rate_mid); sentry_options_set_traces_sample_rate(options, traces_sample_rate); - sentry_init(options); - return; + int rv = sentry_init(options); + return (jint) rv; fail: if (!transport_owns_path) { @@ -366,6 +366,7 @@ Java_io_sentry_ndk_SentryNdk_initSentryNative( sentry_transport_free(transport); } sentry_options_free(options); + return (jint) -1; } JNIEXPORT void JNICALL