diff --git a/src/main/java/cn/hezhaohui/thumb/exception/BusinessException.java b/src/main/java/cn/hezhaohui/thumb/exception/BusinessException.java new file mode 100644 index 0000000..7ddcc91 --- /dev/null +++ b/src/main/java/cn/hezhaohui/thumb/exception/BusinessException.java @@ -0,0 +1,21 @@ +package cn.hezhaohui.thumb.exception; + +public class BusinessException extends RuntimeException{ + + private final int code; + + public BusinessException(int code, String message) { + super(message); + this.code = code; + } + + public BusinessException(ErrorCode errorCode) { + super(errorCode.getMessage()); + this.code = errorCode.getCode(); + } + + public BusinessException(ErrorCode errorCode, String message) { + super(message); + this.code = errorCode.getCode(); + } +} diff --git a/src/main/java/cn/hezhaohui/thumb/exception/ErrorCode.java b/src/main/java/cn/hezhaohui/thumb/exception/ErrorCode.java new file mode 100644 index 0000000..9519f32 --- /dev/null +++ b/src/main/java/cn/hezhaohui/thumb/exception/ErrorCode.java @@ -0,0 +1,32 @@ +package cn.hezhaohui.thumb.exception; + +import lombok.Getter; + +@Getter +public enum ErrorCode { + + SUCCESS(0, "ok"), + PARAMS_ERROR(40000, "请求参数错误"), + NOT_LOGIN_ERROR(40100, "未登录"), + NO_AUTH_ERROR(40101, "无权限"), + NOT_FOUND_ERROR(40400, "请求数据不存在"), + FORBIDDEN_ERROR(40300, "禁止访问"), + SYSTEM_ERROR(50000, "系统内部异常"), + OPERATION_ERROR(50001, "操作失败"); + + /** + * 状态码 + */ + private final int code; + + /** + * 信息 + */ + private final String message; + + ErrorCode(int code, String message) { + this.code = code; + this.message = message; + } + +} diff --git a/src/main/java/cn/hezhaohui/thumb/exception/ThrowUtils.java b/src/main/java/cn/hezhaohui/thumb/exception/ThrowUtils.java new file mode 100644 index 0000000..92506d2 --- /dev/null +++ b/src/main/java/cn/hezhaohui/thumb/exception/ThrowUtils.java @@ -0,0 +1,18 @@ +package cn.hezhaohui.thumb.exception; + +public class ThrowUtils { + + public static void throwIf(boolean condition, RuntimeException runtimeException) { + if (condition) { + throw runtimeException; + } + } + + public static void throwIf(boolean condition, ErrorCode errorCode) { + throwIf(condition, new BusinessException(errorCode)); + } + + public static void throwIf(boolean condition, ErrorCode errorCode, String message) { + throwIf(condition, new BusinessException(errorCode, message)); + } +}