ExceptionHelper.java 1.89 KB
package com.yanzuoguang.util.exception;

import com.yanzuoguang.util.contants.ResultConstants;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.vo.ResponseResult;

/**
 * 异常处理帮助类
 * @author 颜佐光
 */
public class ExceptionHelper {
    /**
     * 不跑出异常,记录日常日志
     *
     * @param cls
     * @param ex
     */
    public static void handleException(Class<?> cls, Throwable ex) {
        handleException(cls, ex, null);
    }

    /**
     * 不跑出异常,记录日常日志
     *
     * @param cls
     * @param ex
     * @param from
     */
    public static void handleException(Class<?> cls, Throwable ex, Object from) {
        ex.printStackTrace();
        // todo: 需要修改
//        Log.error(ExceptionHelper.class, ex, StringHelper.toString(from));
    }

    /**
     * 异常信息输出
     *
     * @param local
     * @param start
     * @param end
     * @param allsize
     * @param str
     * @throws Exception
     */
    public static void subStringException(String local, int start, int end, int allsize, String str) throws Exception {
        String exception = local + "/n" + "开始:" + start + " 结束:" + end + "总长:" + allsize + "/n" + str;
        throw new Exception(exception);
    }


    /**
     * 根据异常获取返回数据
     *
     * @param e 异常信息
     * @return 返回的数据
     */
    public static ResponseResult getError(Exception e) {
        if (e instanceof CodeException) {
            CodeException code = (CodeException) e;
            return new ResponseResult(code.getCode(), code.getMessage(), null, code.getTarget());
        } else {
            String msg = e.getMessage();
            if (StringHelper.isEmpty(msg)) {
                msg = e.getClass().getName();
            }
            return ResponseResult.error(ResultConstants.UNKNOW_ERROR, msg, e);
        }
    }
}