Commit aee91ed5 authored by yanzg's avatar yanzg

修改实例化关系

parent b0a6d5b7
package com.yanzuoguang.util; package com.yanzuoguang.util;
import com.yanzuoguang.util.exception.CodeException; import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.helper.JsonHelper; import com.yanzuoguang.util.exception.RuntimeCodeException;
import com.yanzuoguang.util.helper.StringHelper;
import java.util.HashMap;
import java.util.Map;
/** /**
* 系统错误编码 * 系统错误编码
...@@ -14,27 +10,34 @@ import java.util.Map; ...@@ -14,27 +10,34 @@ import java.util.Map;
*/ */
public class YzgError { public class YzgError {
/** private static YzgErrorData error = new YzgErrorData("999.");
* 错误标记
*/
public static final String TAG = "999.";
/**
* 错误缓存消息
*/
private static Map<String, String> ErrorCode = new HashMap<>();
private static void init() { private static void init() {
add("001", ""); error.add("001", "");
} }
private static void initTemp() { private static void initTemp() {
}
/**
* 抛出异常
*
* @param code
* @param args
*/
public static CodeException throwException(String code, Object... args) {
throw error.getCodeException(null, code, args);
} }
public static void add(String code, String message) { /**
ErrorCode.put(code, message); * 抛出异常
*
* @param code
* @param args
*/
public CodeException throwException(Exception ex, String code, Object... args) {
throw error.getCodeException(ex, code, args);
} }
/** /**
...@@ -43,8 +46,8 @@ public class YzgError { ...@@ -43,8 +46,8 @@ public class YzgError {
* @param code * @param code
* @param args * @param args
*/ */
public static CodeException throwException(String code, Object... args) { public CodeException getException(String code, Object... args) {
throw getException(null, code, args); return error.getCodeException(null, code, args);
} }
/** /**
...@@ -53,8 +56,8 @@ public class YzgError { ...@@ -53,8 +56,8 @@ public class YzgError {
* @param code * @param code
* @param args * @param args
*/ */
public static CodeException throwException(Exception ex, String code, Object... args) { public CodeException getException(Exception ex, String code, Object... args) {
throw getException(ex, code, args); return error.getCodeException(ex, code, args);
} }
/** /**
...@@ -63,8 +66,8 @@ public class YzgError { ...@@ -63,8 +66,8 @@ public class YzgError {
* @param code * @param code
* @param args * @param args
*/ */
public static CodeException getException(String code, Object... args) { public RuntimeCodeException throwRuntimeException(String code, Object... args) {
return getException(null, code, args); throw error.getRuntimeException(null, code, args);
} }
/** /**
...@@ -73,8 +76,8 @@ public class YzgError { ...@@ -73,8 +76,8 @@ public class YzgError {
* @param code * @param code
* @param args * @param args
*/ */
public static CodeException getException(Exception ex, String code, Object... args) { public RuntimeCodeException throwRuntimeException(Exception ex, String code, Object... args) {
return getException(ErrorCode, TAG, ex, code, args); throw error.getRuntimeException(ex, code, args);
} }
/** /**
...@@ -83,24 +86,21 @@ public class YzgError { ...@@ -83,24 +86,21 @@ public class YzgError {
* @param code * @param code
* @param args * @param args
*/ */
public static CodeException getException(Map<String, String> ErrorCode, String tag, Exception ex, String code, Object... args) { public RuntimeCodeException getRuntimeException(String code, Object... args) {
String message = ErrorCode.get(code); return error.getRuntimeException(null, code, args);
if (StringHelper.isEmpty(message)) {
message = "未知错误" + JsonHelper.serialize(args);
} else {
try {
message = String.format(message, args);
} catch (Exception e) {
e.printStackTrace();
}
}
if (ex != null) {
return new CodeException(tag + code, message, ex);
} else {
return new CodeException(tag + code, message);
}
} }
/**
* 抛出异常
*
* @param code
* @param args
*/
public RuntimeCodeException getRuntimeException(Exception ex, String code, Object... args) {
return error.getRuntimeException(ex, code, args);
}
static { static {
init(); init();
initTemp(); initTemp();
......
package com.yanzuoguang.util;
import com.yanzuoguang.util.exception.CodeException;
import com.yanzuoguang.util.exception.RuntimeCodeException;
import com.yanzuoguang.util.helper.JsonHelper;
import com.yanzuoguang.util.helper.StringHelper;
import java.util.HashMap;
import java.util.Map;
/**
* 错误处理
*
* @author 颜佐光
*/
public class YzgErrorData {
/**
* 错误标记
*/
public String TAG = StringHelper.EMPTY;
/**
* 错误缓存消息
*/
private Map<String, String> ErrorCode = new HashMap<>();
public YzgErrorData(String TAG) {
this.TAG = TAG;
}
/**
* 添加错误信息
*
* @param code 错误编码
* @param message 错误消息
*/
public void add(String code, String message) {
ErrorCode.put(code, message);
}
/**
* 抛出异常
*
* @param code
* @param args
*/
public CodeException getCodeException(String code, Object... args) {
return getCodeException(null, code, args);
}
/**
* 抛出异常
*
* @param code
* @param args
*/
public CodeException getCodeException(Exception ex, String code, Object... args) {
String message = getMessage(ErrorCode, code, args);
if (ex != null) {
return new CodeException(TAG + code, message, ex);
} else {
return new CodeException(TAG + code, message);
}
}
/**
* 抛出异常
*
* @param code
* @param args
*/
public RuntimeCodeException getRuntimeException(String code, Object... args) {
return getRuntimeException(null, code, args);
}
/**
* 抛出异常
*
* @param code
* @param args
*/
public RuntimeCodeException getRuntimeException(Exception ex, String code, Object... args) {
String message = getMessage(ErrorCode, code, args);
if (ex != null) {
return new RuntimeCodeException(TAG + code, message, ex);
} else {
return new RuntimeCodeException(TAG + code, message);
}
}
/**
* 获取异常消息
*
* @param errorCode
* @param code
* @param args
* @return
*/
public String getMessage(Map<String, String> errorCode, String code, Object... args) {
String message = errorCode.get(code);
if (StringHelper.isEmpty(message)) {
message = "未知错误" + JsonHelper.serialize(args);
} else {
try {
message = String.format(message, args);
} catch (Exception e) {
e.printStackTrace();
}
}
return message;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment