1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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);
}
}
}