Added more descriptive error to StandardMessageCodec for types that override toString() (flutter/engine#26527)

This commit is contained in:
gaaclarke
2021-06-03 14:44:02 -07:00
committed by GitHub
parent 84d62201b6
commit ec7c6f08d8
2 changed files with 24 additions and 1 deletions

View File

@@ -288,7 +288,8 @@ public class StandardMessageCodec implements MessageCodec<Object> {
writeFloat(stream, f);
}
} else {
throw new IllegalArgumentException("Unsupported value: " + value);
throw new IllegalArgumentException(
"Unsupported value: '" + value + "' of type '" + value.getClass() + "'");
}
}

View File

@@ -2,6 +2,8 @@ package io.flutter.plugin.common;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import android.text.SpannableString;
import java.nio.ByteBuffer;
@@ -122,4 +124,24 @@ public class StandardMessageCodecTest {
String value = (String) codec.decodeMessage(message);
assertEquals(value, "hello world");
}
private static class NotEncodable {
@Override
public String toString() {
return "not encodable";
}
}
@Test
public void errorHasType() {
StandardMessageCodec codec = new StandardMessageCodec();
NotEncodable notEncodable = new NotEncodable();
Exception exception =
assertThrows(
IllegalArgumentException.class,
() -> {
codec.encodeMessage(notEncodable);
});
assertTrue(exception.getMessage().contains("NotEncodable"));
}
}