-
-
Notifications
You must be signed in to change notification settings - Fork 198
feat(ndk): expose init return code #1430
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
base: master
Are you sure you want to change the base?
Conversation
|
@markushi, I think we discussed this some time ago, but I can't remember whether return code or Exception was the choice back then. |
|
Just as info, I made it so that an exception will be thrown by the ndk integration in |
|
But yeah, interestingly i think these sentry-native/ndk/lib/src/main/jni/sentry.c Lines 299 to 302 in 9895a5c
With this change its also sub-optimal, since the developer might turn on sentry-native/src/sentry_core.c Lines 180 to 184 in 9895a5c
? // Edit: okay i don't know if |
|
I think that was the main reason exceptions were raised back then: it allowed clients to differentiate between a native |
|
@sentry review |
| public static int init(@NotNull final NdkOptions options) { | ||
| loadNativeLibraries(); | ||
| initSentryNative(options); | ||
| return initSentryNative(options); | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The public init() method should document the return value semantics for callers. Add a @return javadoc comment explaining that 0 indicates success and non-zero indicates failure, similar to the documentation added to the private native method.
Severity: MEDIUM
💡 Suggested Fix
| public static int init(@NotNull final NdkOptions options) { | |
| loadNativeLibraries(); | |
| initSentryNative(options); | |
| return initSentryNative(options); | |
| } | |
| /** | |
| * Initializes sentry-native and returns 0 on success, non-zero on failure. | |
| * | |
| * @param options the NdkOptions | |
| * @return 0 on success, non-zero on failure | |
| */ | |
| public static int init(@NotNull final NdkOptions options) { |
Did we get this right? 👍 / 👎 to inform future reviews.
| sentry_transport_free(transport); | ||
| } | ||
| sentry_options_free(options); | ||
| return (jint) -1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error handling returns -1 for general initialization failures but returns the actual sentry_init() result on success. This creates inconsistent error code semantics. Consider either: (1) documenting the different error code meanings (what specific non-zero values from sentry_init() mean vs -1 for setup failures), or (2) normalizing the return value to always return -1 for any failure and only 0 for success. This will make error handling more predictable for Java callers.
Severity: MEDIUM
🤖 Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.
Location: ndk/lib/src/main/jni/sentry.c#L358-L369
Potential issue: The error handling returns -1 for general initialization failures but
returns the actual sentry_init() result on success. This creates inconsistent error code
semantics. Consider either: (1) documenting the different error code meanings (what
specific non-zero values from sentry_init() mean vs -1 for setup failures), or (2)
normalizing the return value to always return -1 for any failure and only 0 for success.
This will make error handling more predictable for Java callers.
Did we get this right? 👍 / 👎 to inform future reviews.
|
@hannojg thanks for opening this up! This is definitely an area of improvement and I think using a return code is the way to go right now. It would also allow some flexibility, e.g. returning some positive integer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks again you for contributing, much appreciated! I left a comment on how far the return code should be propagated up. If something is unclear, just let me know.
| public static int init(@NotNull final NdkOptions options) { | ||
| loadNativeLibraries(); | ||
| initSentryNative(options); | ||
| return initSentryNative(options); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should keep using void here and throw an exception within init if the error code is non-0. This keeps API compatibility and ties the error code handling closer to the actual C code.
We also don't need any extra handling within sentry-java then, as it's covered by this try-catch here.
| public static int init(@NotNull final NdkOptions options) { | |
| loadNativeLibraries(); | |
| initSentryNative(options); | |
| return initSentryNative(options); | |
| } | |
| public static void init(@NotNull final NdkOptions options) { | |
| loadNativeLibraries(); | |
| final int returnCode = initSentryNative(options); | |
| if (returnCode != 0) { | |
| throw new IllegalStateException("Failed to initialize sentry-native, return code: " + returnCode); | |
| } | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would even go one step further here and check whether the return code was 1 (in which case there is some native-internal issue which could be resolved via logs and there is no need to render a return code) and having an enum range in the negative return code range, that results in an explanatory expection message for each particular return code.
Even if we don't go that far, we should at least differentiate between > 0 and < 0 returnCode exceptions, to clarify where the error happened (in the JNI layer or native proper). Since the primary failure mode in the JNI layer is about allocation, most exceptions from there would not be actionable to client code. However, having a basic differentiation between the source of the exception, would still be useful.
|
Just my 2c here:
|
Return int from SentryNdk.init reflecting sentry_init result.
This can then be used by sentry-java/android to double check if the Ndk integration could be initialized successfully (which currently isn't happening, we just call the native method and assume it went successful).
Upstream PR in
getsentry/sentry-javafor android ndk: