##### Coverage Report - com.ctrip.framework.apollo.util.ExceptionUtil

Classes in this File | Line Coverage | Branch Coverage | Complexity
ExceptionUtil |
95% | 20/21
91% | 11/12
| 8.0;8

1 |  |
     package com.ctrip.framework.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 | 68 |
         if (ex == null || Strings.isNullOrEmpty(ex.getMessage())) {
19 | 3 |
           return "";
20 |  |
         }
21 | 65 |
         StringBuilder builder = new StringBuilder(ex.getMessage());
22 | 65 |
         List<Throwable> causes = Lists.newLinkedList();
23 |  |
24 | 65 |
         int counter = 0;
25 | 65 |
         Throwable current = ex;
26 |  |
         //retrieve up to 10 causes
27 | 103 |
         while (current.getCause() != null && counter < 10) {
28 | 38 |
           Throwable next = current.getCause();
29 | 38 |
           causes.add(next);
30 | 38 |
           current = next;
31 | 38 |
           counter++;
32 | 38 |
         }
33 |  |
34 | 65 |
         for (Throwable cause : causes) {
35 | 38 |
           if (Strings.isNullOrEmpty(cause.getMessage())) {
36 | 4 |
             counter--;
37 | 4 |
             continue;
38 |  |
           }
39 | 34 |
           builder.append(" [Cause: ").append(cause.getMessage());
40 | 34 |
         }
41 |  |
42 | 65 |
         builder.append(Strings.repeat("]", counter));
43 |  |
44 | 65 |
         return builder.toString();
45 |  |
       }
46 |  |
     }