| 1 | |
package com.ctrip.apollo.common.utils; |
| 2 | |
|
| 3 | |
import java.lang.reflect.Type; |
| 4 | |
import java.util.Map; |
| 5 | |
|
| 6 | |
import org.springframework.web.client.HttpStatusCodeException; |
| 7 | |
|
| 8 | |
import com.google.common.base.MoreObjects; |
| 9 | |
import com.google.gson.Gson; |
| 10 | |
import com.google.gson.reflect.TypeToken; |
| 11 | |
|
| 12 | 0 | public final class ExceptionUtils { |
| 13 | |
|
| 14 | 0 | private static Gson gson = new Gson(); |
| 15 | |
|
| 16 | 0 | private static Type mapType = new TypeToken<Map<String, Object>>() {}.getType(); |
| 17 | |
|
| 18 | |
public static String toString(HttpStatusCodeException e) { |
| 19 | 0 | Map<String, Object> errorAttributes = gson.fromJson(e.getResponseBodyAsString(), mapType); |
| 20 | 0 | if (errorAttributes != null) { |
| 21 | 0 | return MoreObjects.toStringHelper(HttpStatusCodeException.class).omitNullValues() |
| 22 | 0 | .add("status", errorAttributes.get("status")) |
| 23 | 0 | .add("message", errorAttributes.get("message")) |
| 24 | 0 | .add("timestamp", errorAttributes.get("timestamp")) |
| 25 | 0 | .add("exception", errorAttributes.get("exception")) |
| 26 | 0 | .add("errorCode", errorAttributes.get("errorCode")) |
| 27 | 0 | .add("stackTrace", errorAttributes.get("stackTrace")).toString(); |
| 28 | |
} |
| 29 | 0 | return ""; |
| 30 | |
} |
| 31 | |
} |