BusinessException.java
2.39 KB
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
package com.glsx.usercenter.exception;
import com.glsx.common.constants.ResultObject;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.ObjectUtils;
/**
* 系统业务异常,异常号段为 :
* 0 : 成功
* 1 ~ 9999 内定系统异常段
* 10000 ~ 99999 自定义异常码段
* 100000 ~ Integer.MAX_VALUE 动态异常码段
*/
public class BusinessException extends RuntimeException {
protected static final int MIN_AUTO_CODE = 100000;
protected ResultObject resultEntity;
protected BusinessException(BusinessException parent) {
if (parent != null) {
this.resultEntity = parent.resultEntity;
}
}
public static BusinessException create(String message) {
int value = (int) (MIN_AUTO_CODE + Math.round((Integer.MAX_VALUE - MIN_AUTO_CODE) * Math.random()));
return create(value, message);
}
public static BusinessException create(int returnCode, String message) {
BusinessException businessException = new BusinessException(null);
businessException.resultEntity = ResultObject.err(returnCode).message(message);
return businessException;
}
public static BusinessException create(ResultObject resultEntity) {
BusinessException businessException = new BusinessException(null);
businessException.resultEntity = resultEntity;
return businessException;
}
public static BusinessException create(ExceptionCause exceptionCause, Object... args) {
ResultObject resultEntity = exceptionCause.result();
String message = resultEntity.getMessage();
if (ArrayUtils.isNotEmpty(args)) {
String[] argsStringArray = new String[args.length];
for (int i = 0; i < args.length; i++) {
Object arg = args[i];
argsStringArray[i] = ObjectUtils.toString(arg);
}
String formatMessage = String.format(message, argsStringArray);
resultEntity.setMessage(formatMessage);
}
BusinessException businessException = new BusinessException(null);
businessException.resultEntity = resultEntity;
return businessException;
}
@Override
public String getMessage() {
return resultEntity.getMessage();
}
public ResultObject getResultEntity() {
return resultEntity;
}
}