Give even better error messages on failure to launch ahat.

In particular, explicitly state that a heap dump "does not appear to
be a valid Java heap dump" on HprofFormatException.

Bug: 64318055
Test: m ahat-test
Test: ahat Android.mk
Test: ahat FileThatDoesNotExists.hprof
Test: ahat etc/L.hprof & ahat etc/O.hprof

Change-Id: I014b465e23e48941655bcbb3505cec8fd7965fc9
diff --git a/tools/ahat/src/main/com/android/ahat/Main.java b/tools/ahat/src/main/com/android/ahat/Main.java
index 31c485d..a0fbf77 100644
--- a/tools/ahat/src/main/com/android/ahat/Main.java
+++ b/tools/ahat/src/main/com/android/ahat/Main.java
@@ -48,6 +48,26 @@
     out.println("");
   }
 
+  /**
+   * Load the given heap dump file.
+   * Prints an error message and exits the application on failure to load the
+   * heap dump.
+   */
+  private static AhatSnapshot loadHeapDump(File hprof, ProguardMap map) {
+    System.out.println("Processing '" + hprof + "' ...");
+    try {
+      return Parser.parseHeapDump(hprof, map);
+    } catch (IOException e) {
+      System.err.println("Unable to load '" + hprof + "':");
+      e.printStackTrace();
+    } catch (HprofFormatException e) {
+      System.err.println("'" + hprof + "' does not appear to be a valid Java heap dump:");
+      e.printStackTrace();
+    }
+    System.exit(1);
+    throw new AssertionError("Unreachable");
+  }
+
   public static void main(String[] args) {
     int port = 7100;
     for (String arg : args) {
@@ -105,40 +125,39 @@
       return;
     }
 
+    // Launch the server before parsing the hprof file so we get
+    // BindExceptions quickly.
+    InetAddress loopback = InetAddress.getLoopbackAddress();
+    InetSocketAddress addr = new InetSocketAddress(loopback, port);
+    System.out.println("Preparing " + addr + " ...");
+    HttpServer server = null;
     try {
-      // Launch the server before parsing the hprof file so we get
-      // BindExceptions quickly.
-      InetAddress loopback = InetAddress.getLoopbackAddress();
-      InetSocketAddress addr = new InetSocketAddress(loopback, port);
-      System.out.println("Preparing " + addr + " ...");
-      HttpServer server = HttpServer.create(addr, 0);
-
-      System.out.println("Processing '" + hprof + "' ...");
-      AhatSnapshot ahat = Parser.parseHeapDump(hprof, map);
-
-      if (hprofbase != null) {
-        System.out.println("Processing '" + hprofbase + "' ...");
-        AhatSnapshot base = Parser.parseHeapDump(hprofbase, mapbase);
-
-        System.out.println("Diffing heap dumps ...");
-        Diff.snapshots(ahat, base);
-      }
-
-      server.createContext("/", new AhatHttpHandler(new OverviewHandler(ahat, hprof, hprofbase)));
-      server.createContext("/rooted", new AhatHttpHandler(new RootedHandler(ahat)));
-      server.createContext("/object", new AhatHttpHandler(new ObjectHandler(ahat)));
-      server.createContext("/objects", new AhatHttpHandler(new ObjectsHandler(ahat)));
-      server.createContext("/site", new AhatHttpHandler(new SiteHandler(ahat)));
-      server.createContext("/bitmap", new BitmapHandler(ahat));
-      server.createContext("/style.css", new StaticHandler("style.css", "text/css"));
-      server.setExecutor(Executors.newFixedThreadPool(1));
-      System.out.println("Server started on localhost:" + port);
-
-      server.start();
-    } catch (HprofFormatException|IOException e) {
-      System.err.println("Unable to launch ahat:");
+      server = HttpServer.create(addr, 0);
+    } catch (IOException e) {
+      System.err.println("Unable to setup ahat server:");
       e.printStackTrace();
+      System.exit(1);
     }
+
+    AhatSnapshot ahat = loadHeapDump(hprof, map);
+    if (hprofbase != null) {
+      AhatSnapshot base = loadHeapDump(hprofbase, mapbase);
+
+      System.out.println("Diffing heap dumps ...");
+      Diff.snapshots(ahat, base);
+    }
+
+    server.createContext("/", new AhatHttpHandler(new OverviewHandler(ahat, hprof, hprofbase)));
+    server.createContext("/rooted", new AhatHttpHandler(new RootedHandler(ahat)));
+    server.createContext("/object", new AhatHttpHandler(new ObjectHandler(ahat)));
+    server.createContext("/objects", new AhatHttpHandler(new ObjectsHandler(ahat)));
+    server.createContext("/site", new AhatHttpHandler(new SiteHandler(ahat)));
+    server.createContext("/bitmap", new BitmapHandler(ahat));
+    server.createContext("/style.css", new StaticHandler("style.css", "text/css"));
+    server.setExecutor(Executors.newFixedThreadPool(1));
+    System.out.println("Server started on localhost:" + port);
+
+    server.start();
   }
 }