Coverage Report - com.ctrip.apollo.util.ExceptionUtil
 
Classes in this File Line Coverage Branch Coverage Complexity
ExceptionUtil
95%
20/21
91%
11/12
8
 
 1  
 package com.ctrip.apollo.util;
 2  
 
 3  
 import com.google.common.base.Strings;
 4  
 import com.google.common.collect.Lists;
 5  
 
 6  
 import java.util.List;
 7  
 
 8  
 /**
 9  
  * @author Jason Song(song_s@ctrip.com)
 10  
  */
 11  0
 public class ExceptionUtil {
 12  
   /**
 13  
    * Assemble the detail message for the throwable with all of its cause included (at most 10 causes).
 14  
    * @param ex the exception
 15  
    * @return the message along with its causes
 16  
    */
 17  
   public static String getDetailMessage(Throwable ex) {
 18  55
     if (ex == null || Strings.isNullOrEmpty(ex.getMessage())) {
 19  3
       return "";
 20  
     }
 21  52
     StringBuilder builder = new StringBuilder(ex.getMessage());
 22  52
     List<Throwable> causes = Lists.newLinkedList();
 23  
 
 24  52
     int counter = 0;
 25  52
     Throwable current = ex;
 26  
     //retrieve up to 10 causes
 27  82
     while (current.getCause() != null && counter < 10) {
 28  30
       Throwable next = current.getCause();
 29  30
       causes.add(next);
 30  30
       current = next;
 31  30
       counter++;
 32  30
     }
 33  
 
 34  52
     for (Throwable cause : causes) {
 35  30
       if (Strings.isNullOrEmpty(cause.getMessage())) {
 36  1
         counter--;
 37  1
         continue;
 38  
       }
 39  29
       builder.append(" [Cause: ").append(cause.getMessage());
 40  29
     }
 41  
 
 42  52
     builder.append(Strings.repeat("]", counter));
 43  
 
 44  52
     return builder.toString();
 45  
   }
 46  
 }