From ec7c6f08d8ef5701a152360bb7d32d3e2734292d Mon Sep 17 00:00:00 2001 From: gaaclarke <30870216+gaaclarke@users.noreply.github.com> Date: Thu, 3 Jun 2021 14:44:02 -0700 Subject: [PATCH] Added more descriptive error to StandardMessageCodec for types that override toString() (flutter/engine#26527) --- .../plugin/common/StandardMessageCodec.java | 3 ++- .../common/StandardMessageCodecTest.java | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/common/StandardMessageCodec.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/common/StandardMessageCodec.java index e2a5bc7cef..8050292174 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/plugin/common/StandardMessageCodec.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/common/StandardMessageCodec.java @@ -288,7 +288,8 @@ public class StandardMessageCodec implements MessageCodec { writeFloat(stream, f); } } else { - throw new IllegalArgumentException("Unsupported value: " + value); + throw new IllegalArgumentException( + "Unsupported value: '" + value + "' of type '" + value.getClass() + "'"); } } diff --git a/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/common/StandardMessageCodecTest.java b/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/common/StandardMessageCodecTest.java index 96f7908848..4f7f14a635 100644 --- a/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/common/StandardMessageCodecTest.java +++ b/engine/src/flutter/shell/platform/android/test/io/flutter/plugin/common/StandardMessageCodecTest.java @@ -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")); + } }