[go: nahoru, domu]

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

☂️ Exceptions #561

Open
1 of 3 tasks
HosseinYousefi opened this issue Aug 29, 2023 · 2 comments
Open
1 of 3 tasks

☂️ Exceptions #561

HosseinYousefi opened this issue Aug 29, 2023 · 2 comments

Comments

@HosseinYousefi
Copy link
Member
HosseinYousefi commented Aug 29, 2023

What if someone wants to write some custom Java interface that is aware of the fact that it's going to be used in Dart. Maybe in this case the developer would want to catch Dart exceptions which we currently don't allow. I personally don't think this is of the highest priority. (Discussion dart-archive/jnigen#376 (comment))

  • Support handling DartExceptions in custom code

Another thing that users might want to do is throw actual Java exceptions. This seems to be useful actually, and we could use the same Dart throw keyword to achieve this by checking if the exception thrown is of type JException.

However currently JException doesn't inherit JObject, we can change this by adding the Throwable and Exception types as core types to package:jni.

@HosseinYousefi HosseinYousefi transferred this issue from dart-archive/jnigen Nov 17, 2023
parlough pushed a commit to parlough/native that referenced this issue Apr 8, 2024
* Handle multi anonymous struct with same USR (dart-lang#559)

Co-authored-by: Daco Harkes <dacoharkes@google.com>
@HosseinYousefi HosseinYousefi added this to the JNI / JNIgen 0.15.0 milestone May 22, 2024
@HosseinYousefi HosseinYousefi self-assigned this May 22, 2024
@Anikate-De
Copy link
Contributor

Hi! This is in the context of the upcoming ok_http package using jnigen for the Kotlin package OkHttp. Specifically this PR

This is the problem we faced:

  • To properly implement conformant redirection logic, we needed to use an interface Interceptor, and further, needed to throw a java.io.IOException, in the intercept function of the implementation
  • We tried throwing this exception (after generating bindings of IOException) using the Dart throw keyword, but that led to a crash with: java.lang.reflect.UndeclaredThrowableException
  • Neither did throwing a Dart exception work out for us. I'm unsure of how the JNI threading works, but it has always led to a deadlock in this case. And, while making requests, you would eventually face a java.net.SocketTimeoutException as it's OkHttp's predicted behaviour.

Workaround:

  • We created a Kotlin function addRedirectInterceptor that implements the said logic.
  • Generated bindings for this function, and called it from the Dart end.
  • Therefore, we basically, removed the JNI interop to throw IOException and decided to deal with it natively.

(Just in case someone faces the same problem and looks for how others dealt with it)

Would there have been a better way to resolve this?
This comment is result of the PR review: Link

TL;DR - Faced problems with throwing exceptions within interface implementations using package:jni (both Dart & Java Exceptions) -> Lead to a Deadlock -> Decided to switch to native Kotlin code to deal with the exception-throwing logic -> Used the bindings of this custom class.

@HosseinYousefi
Copy link
Member Author

The problem is all thrown exceptions are currently wrapped in a DartException class, even Java exceptions.

You can fix this if you have time: #1209. I added some code pointers there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Todo
Development

No branches or pull requests

2 participants