mobile/bind: use objects to pass errors across the language barrier

Gobind uses strings for passing errors across the language barrier.
However, since Gobind doesn't have a concept of a nil string, it
can't separate an empty native string from a nil string.

In turn, that means that empty errors, exceptions or NSError * with
an empty description are treated as no error. With ObjC, empty errors
are replaced with a default string to workaround the issue, while
with Java empty errors are silently ignored.

Fix this by replacing strings with actual error objects, wrapping
the Go error, Java Throwable or ObjC NSError *, and letting the
existing bind machinery take care of passing the references across.

It's a large change for a small corner case, but I believe objects
are a better fit for exception that strings. Error objects also
naturally leads to future additions, for example accessing the
exception class name or chained exception.

Change-Id: Ie03b47cafcb231ad1e12a80195693fa7459c6265
Reviewed-on: https://go-review.googlesource.com/24100
Reviewed-by: David Crawshaw <crawshaw@golang.org>
This commit is contained in:
Elias Naur
2016-06-23 18:55:48 +00:00
parent 3465f91246
commit a3e0621280
48 changed files with 552 additions and 346 deletions
+13 -2
View File
@@ -8,6 +8,8 @@ import java.util.Arrays;
import java.util.IdentityHashMap;
import java.util.logging.Logger;
import go.Universe;
// Seq is a sequence of machine-dependent encoded values.
// Used by automatically generated language bindings to talk to Go.
public class Seq {
@@ -34,6 +36,7 @@ public class Seq {
log.severe("LoadJNI class bad field: " + e);
}
init();
Universe.touch();
}
private static native void init();
@@ -44,8 +47,16 @@ public class Seq {
private Seq() {
}
private static void throwException(String msg) throws Exception { // JNI helper
throw new Exception(msg);
private static void throwException(Universe.error err) throws Exception {
throw new Exception(err.Error());
}
private static Universe.error wrapThrowable(final Throwable t) {
return new Universe.error() {
@Override public String Error() {
return t.getMessage();
}
};
}
// ctx is an android.context.Context.