| 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 | |
|
| 10 | |
|
| 11 | 0 | public class ExceptionUtil { |
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 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 | |
|
| 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 | |
} |