|
@@ -0,0 +1,86 @@
|
|
|
+package com.dayou.exception;
|
|
|
+
|
|
|
+import org.apache.commons.lang3.ArrayUtils;
|
|
|
+import org.apache.commons.lang3.builder.ToStringBuilder;
|
|
|
+
|
|
|
+import java.util.Objects;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Define the error id.
|
|
|
+ */
|
|
|
+public class ErrorCode {
|
|
|
+ public static final ErrorCode CUSTOM_ERROR = ErrorCode("10010", "{0}");
|
|
|
+ public static final ErrorCode DEFAULTERROR = ErrorCode("10001", "系统繁忙");
|
|
|
+ public static final ErrorCode DATA_NOT_EXISTS = ErrorCode("004", "数据不存在");
|
|
|
+
|
|
|
+
|
|
|
+ private String code;
|
|
|
+ private String errorMsg;
|
|
|
+
|
|
|
+ public static ErrorCode ErrorCode(String code, String errorMsg) {
|
|
|
+ return new ErrorCode(code, errorMsg);
|
|
|
+ }
|
|
|
+
|
|
|
+ private ErrorCode() {
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public ErrorCode(String code, String errorMsg) {
|
|
|
+ this.code = code;
|
|
|
+ this.errorMsg = errorMsg;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void throwBusinessException(ErrorCode errorCode, Object... values) {
|
|
|
+ throw buildBusinessException(errorCode, values);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static BusinessException buildBusinessException(ErrorCode errorCode, Object... values) {
|
|
|
+ String message = errorCode.errorMsg;
|
|
|
+ if (ArrayUtils.isNotEmpty(values)) {
|
|
|
+ for (int i = 0; i < values.length; i++) {
|
|
|
+ if (message.contains("{") && message.contains("}")) {
|
|
|
+ message = message.replaceFirst("\\{" + i + "\\}", values[i].toString());
|
|
|
+ } else {
|
|
|
+ message += " " + values[i].toString() + " ";
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return new BusinessException(message, errorCode.code);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void throwBusinessException(String msg, String code) {
|
|
|
+ throw new BusinessException(msg, code == null ? DEFAULTERROR.getCode() : code);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public String getCode() {
|
|
|
+ return code;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getErrorMsg() {
|
|
|
+ return errorMsg;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String toString() {
|
|
|
+ return new ToStringBuilder(this)
|
|
|
+ .append("id", code)
|
|
|
+ .append("errorMsg", errorMsg)
|
|
|
+ .toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setCode(String code) {
|
|
|
+ this.code = code;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setErrorMsg(String errorMsg) {
|
|
|
+ this.errorMsg = errorMsg;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void throwExceptionIfNull(Object o){
|
|
|
+ if(Objects.isNull(o)){
|
|
|
+ throwBusinessException(DATA_NOT_EXISTS);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|