异常处理工具类

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.ArrayUtils;

/**
 * 异常处理工具类
 *
 */
public final class HandleExceptionUtils {
    private HandleExceptionUtils(){}

    /**
     * 异常处理方法
     *
     * @param throwable 异常
     * @return
     */
    public static String handleException(Throwable throwable) {
        JSONObject errorJson = new JSONObject();
        StackTraceElement[] stackTraceArray = throwable.getStackTrace();
        errorJson.put("exceptionType", throwable.getClass().getName());
        errorJson.put("exceptionMessage", throwable.getLocalizedMessage());
        if (ArrayUtils.isNotEmpty(stackTraceArray)) {
            int maxSize = Math.min(stackTraceArray.length, 20);
            JSONArray array = new JSONArray();
            for (int i = 0; i < maxSize; i++) {
                StackTraceElement stackTraceElement = stackTraceArray[i];
          
上一篇:java解析Json中获取Array字段值及嵌套Json对象


下一篇:Zuul网关之解析重组GET\POST\PUT请求并支持contentType=“multipart/form-data”