changeset 167:fb425a4e5af7

Bug 3001: HeapStats CLI should report error without exception Reviewed-by: ykubota https://github.com/HeapStats/heapstats/pull/38
author Yasumasa Suenaga <yasuenag@gmail.com>
date Tue, 11 Oct 2016 22:51:44 +0900
parents cf00c05c2431
children b3f6e49a5c7a
files ChangeLog analyzer/cli/src/main/java/jp/co/ntt/oss/heapstats/cli/CliMain.java
diffstat 2 files changed, 24 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Tue Oct 11 21:29:15 2016 +0900
+++ b/ChangeLog	Tue Oct 11 22:51:44 2016 +0900
@@ -1,6 +1,7 @@
 2016-10-06  Yasumasa Suenaga <yasuenag@gmail.com>
 
 	* Bug 3002: HeapStats CLI should show ID in each subcommand
+	* Bug 3001: HeapStats CLI should report error without exception
 
 2016-10-07  KUBOTA Yuji <kubota.yuji@lab.ntt.co.jp>
 
--- a/analyzer/cli/src/main/java/jp/co/ntt/oss/heapstats/cli/CliMain.java	Tue Oct 11 21:29:15 2016 +0900
+++ b/analyzer/cli/src/main/java/jp/co/ntt/oss/heapstats/cli/CliMain.java	Tue Oct 11 22:51:44 2016 +0900
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015 Yasumasa Suenaga
+ * Copyright (C) 2015-2016 Yasumasa Suenaga
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License
@@ -17,6 +17,7 @@
  */
 package jp.co.ntt.oss.heapstats.cli;
 
+import java.util.Optional;
 import jp.co.ntt.oss.heapstats.cli.processor.CliProcessor;
 import jp.co.ntt.oss.heapstats.cli.processor.JMXProcessor;
 import jp.co.ntt.oss.heapstats.cli.processor.LogProcessor;
@@ -28,13 +29,14 @@
  * 
  * @author Yasumasa Suenaga
  */
-public class CliMain {
+public class CliMain implements Thread.UncaughtExceptionHandler{
 
     /**
      * @param args the command line arguments
      */
     public static void main(String[] args) {
         Options options = new Options();
+        Thread.setDefaultUncaughtExceptionHandler(new CliMain());
         
         try{
             options.parse(args);
@@ -71,5 +73,24 @@
         
         processor.process();        
     }
+
+    @Override
+    public void uncaughtException(Thread thread, Throwable thrwbl) {
+        Throwable rootCause = thrwbl;
+        
+        // Find root cause of exception.
+        // Exceptions is recuesive. So we need to track exception(s) through Throwable#getCause().
+        while(rootCause.getCause() != null){
+            rootCause = rootCause.getCause();
+        }
+        
+        String message = Optional.ofNullable(rootCause.getLocalizedMessage())
+                                 .orElse(rootCause.toString());        
+        System.err.println("HeapStats CLI error: " + message);
+        if(Boolean.getBoolean("debug")){
+            thrwbl.printStackTrace();
+        }
+
+    }
     
 }