Improve error reporting for SdkExtensionsHostTest.broadcast(...)

Adds `checkExitCode(...)` method that will include the command, exit
code, stderr and stdout in the exception message when the exit code is
non-zero.

Bug: 332479207
Test: forrest
Change-Id: If8aa5631a12a6a6c459bdeee14415e85514e7970
diff --git a/javatests/com/android/sdkext/extensions/SdkExtensionsHostTest.java b/javatests/com/android/sdkext/extensions/SdkExtensionsHostTest.java
index 1b1a4e1..0d9d648 100644
--- a/javatests/com/android/sdkext/extensions/SdkExtensionsHostTest.java
+++ b/javatests/com/android/sdkext/extensions/SdkExtensionsHostTest.java
@@ -142,19 +142,34 @@
     private String getExtensionVersionFromSysprop(String v) throws Exception {
         String command = "getprop build.version.extensions." + v;
         CommandResult res = getDevice().executeShellV2Command(command);
-        assertEquals(0, (int) res.getExitCode());
+        checkExitCode(command, res);
         return res.getStdout().replace("\n", "");
     }
 
     private String broadcast(String action, String extra) throws Exception {
         String command = getBroadcastCommand(action, extra);
         CommandResult res = getDevice().executeShellV2Command(command);
-        assertEquals(0, (int) res.getExitCode());
+        checkExitCode(command, res);
         Matcher matcher = Pattern.compile("data=\"([^\"]+)\"").matcher(res.getStdout());
         assertTrue("Unexpected output from am broadcast: " + res.getStdout(), matcher.find());
         return matcher.group(1);
     }
 
+    private static void checkExitCode(String command, CommandResult res) {
+        int exitCode = (int) res.getExitCode();
+        if (exitCode != 0) {
+            throw new IllegalStateException(
+                    String.format(
+                            "Unexpected result from `%s`\n"
+                                    + "    exitCode=%d\n"
+                                    + "    stderr=\n"
+                                    + "%s\n"
+                                    + "    stdout=\n"
+                                    + "%s\n",
+                            command, exitCode, res.getStderr(), res.getStdout()));
+        }
+    }
+
     private boolean broadcastForBoolean(String action, String extra) throws Exception {
         String result = broadcast(action, extra);
         if (result.equals("true") || result.equals("false")) {