Handle Throwable in StandardMethodCodec (flutter/engine#18490)

This commit is contained in:
Mehmet Fidanboylu 2020-05-19 15:15:49 -07:00 committed by GitHub
parent 933fcc1048
commit 2fcffb2046
2 changed files with 20 additions and 3 deletions

View File

@ -165,7 +165,9 @@ public final class MethodChannel {
/**
* Handles a successful result.
*
* @param result The result, possibly null.
* @param result The result, possibly null. The result must be an Object type supported by the
* codec. For instance, if you are using StandardCodec (default), please see {@link
* StandardMessageCodec} documentation on what types are supported.
*/
@UiThread
void success(@Nullable Object result);
@ -175,7 +177,9 @@ public final class MethodChannel {
*
* @param errorCode An error code String.
* @param errorMessage A human-readable error message String, possibly null.
* @param errorDetails Error details, possibly null
* @param errorDetails Error details, possibly null. The details must be an Object type
* supported by the codec. For instance, if you are using StandardCodec (default), please
* see {@link StandardMessageCodec} documentation on what types are supported.
*/
@UiThread
void error(String errorCode, @Nullable String errorMessage, @Nullable Object errorDetails);

View File

@ -5,6 +5,9 @@
package io.flutter.plugin.common;
import io.flutter.plugin.common.StandardMessageCodec.ExposedByteArrayOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
@ -66,7 +69,11 @@ public final class StandardMethodCodec implements MethodCodec {
stream.write(1);
messageCodec.writeValue(stream, errorCode);
messageCodec.writeValue(stream, errorMessage);
messageCodec.writeValue(stream, errorDetails);
if (errorDetails instanceof Throwable) {
messageCodec.writeValue(stream, getStackTrace((Throwable) errorDetails));
} else {
messageCodec.writeValue(stream, errorDetails);
}
final ByteBuffer buffer = ByteBuffer.allocateDirect(stream.size());
buffer.put(stream.buffer(), 0, stream.size());
return buffer;
@ -99,4 +106,10 @@ public final class StandardMethodCodec implements MethodCodec {
}
throw new IllegalArgumentException("Envelope corrupted");
}
private static String getStackTrace(Throwable t) {
Writer result = new StringWriter();
t.printStackTrace(new PrintWriter(result));
return result.toString();
}
}