20230219
This commit is contained in:
parent
92dbc613d2
commit
9293ba6817
@ -0,0 +1,28 @@
|
||||
package xyz.zhouxy.plusone.exception.handler;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import xyz.zhouxy.plusone.exception.SysException;
|
||||
import xyz.zhouxy.plusone.util.RestfulResult;
|
||||
|
||||
@RestControllerAdvice
|
||||
@Slf4j
|
||||
public class SysExceptionHandler extends BaseExceptionHandler {
|
||||
|
||||
public SysExceptionHandler(ExceptionInfoHolder exceptionInfoHolder) {
|
||||
super(exceptionInfoHolder);
|
||||
}
|
||||
|
||||
@ExceptionHandler({ SysException.class })
|
||||
public ResponseEntity<RestfulResult> handleException(@Nonnull Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
HttpStatus httpStatus = getHttpStatus(e);
|
||||
return new ResponseEntity<>(RestfulResult.error(getErrorCode(e), "系统错误"), httpStatus);
|
||||
}
|
||||
}
|
@ -39,12 +39,12 @@
|
||||
<dependency>
|
||||
<groupId>xyz.zhouxy.plusone</groupId>
|
||||
<artifactId>plusone-commons</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>xyz.zhouxy.plusone</groupId>
|
||||
<artifactId>plusone-validator</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<version>0.1.2-SNAPSHOT</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>xyz.zhouxy.plusone</groupId>
|
||||
|
@ -0,0 +1,37 @@
|
||||
package xyz.zhouxy.plusone.exception;
|
||||
|
||||
/**
|
||||
* 业务异常
|
||||
*
|
||||
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
||||
*/
|
||||
public class BizException extends BaseException {
|
||||
@java.io.Serial
|
||||
private static final long serialVersionUID = -5524759033245815405L;
|
||||
|
||||
public static final int DEFAULT_ERROR_CODE = 4000000;
|
||||
|
||||
public BizException(int code, String msg) {
|
||||
super(code, msg);
|
||||
}
|
||||
|
||||
public BizException(int code, Throwable cause) {
|
||||
super(code, cause);
|
||||
}
|
||||
|
||||
public BizException(int code, String msg, Throwable cause) {
|
||||
super(code, msg, cause);
|
||||
}
|
||||
|
||||
public BizException(String msg) {
|
||||
super(DEFAULT_ERROR_CODE, msg);
|
||||
}
|
||||
|
||||
public BizException(Throwable cause) {
|
||||
super(DEFAULT_ERROR_CODE, cause);
|
||||
}
|
||||
|
||||
public BizException(String msg, Throwable cause) {
|
||||
super(DEFAULT_ERROR_CODE, msg, cause);
|
||||
}
|
||||
}
|
@ -9,7 +9,7 @@ import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
||||
*/
|
||||
@ResponseStatus(HttpStatus.NOT_FOUND)
|
||||
public class DataNotExistException extends PlusoneException {
|
||||
public class DataNotExistException extends BizException {
|
||||
|
||||
@java.io.Serial
|
||||
private static final long serialVersionUID = 6536955800679703111L;
|
||||
|
@ -8,13 +8,12 @@ import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
*
|
||||
* <p>
|
||||
* 暂时先这样,后续完善异常体系时可能会更改。
|
||||
* </p>
|
||||
*
|
||||
* @author <a href="https://gitee.com/zhouxy108">ZhouXY</a>
|
||||
* @see xyz.zhouxy.plusone.util.AssertResult
|
||||
*/
|
||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
public class DataOperationResultException extends PlusoneException {
|
||||
public class DataOperationResultException extends SysException {
|
||||
|
||||
@java.io.Serial
|
||||
private static final long serialVersionUID = -9220765735990318186L;
|
||||
|
@ -0,0 +1,32 @@
|
||||
package xyz.zhouxy.plusone.exception;
|
||||
|
||||
public class SysException extends BaseException {
|
||||
@java.io.Serial
|
||||
private static final long serialVersionUID = 8821240827443168118L;
|
||||
|
||||
public static final int DEFAULT_ERROR_CODE = 5000000;
|
||||
|
||||
public SysException(int code, String msg) {
|
||||
super(code, msg);
|
||||
}
|
||||
|
||||
public SysException(int code, Throwable cause) {
|
||||
super(code, cause);
|
||||
}
|
||||
|
||||
public SysException(int code, String msg, Throwable cause) {
|
||||
super(code, msg, cause);
|
||||
}
|
||||
|
||||
public SysException(String msg) {
|
||||
super(DEFAULT_ERROR_CODE, msg);
|
||||
}
|
||||
|
||||
public SysException(Throwable cause) {
|
||||
super(DEFAULT_ERROR_CODE, cause);
|
||||
}
|
||||
|
||||
public SysException(String msg, Throwable cause) {
|
||||
super(DEFAULT_ERROR_CODE, msg, cause);
|
||||
}
|
||||
}
|
@ -9,7 +9,7 @@ import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
* @author ZhouXY
|
||||
*/
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
public class UserOperationException extends PlusoneException {
|
||||
public class UserOperationException extends BizException {
|
||||
|
||||
@java.io.Serial
|
||||
private static final long serialVersionUID = 4371055414421991940L;
|
||||
|
@ -1,7 +1,6 @@
|
||||
package xyz.zhouxy.plusone.constant;
|
||||
|
||||
import xyz.zhouxy.plusone.util.Enumeration;
|
||||
import xyz.zhouxy.plusone.util.EnumerationValuesHolder;
|
||||
|
||||
/**
|
||||
* 实体状态
|
||||
@ -19,7 +18,8 @@ public class EntityStatus extends Enumeration<EntityStatus> {
|
||||
public static final EntityStatus DISABLED = new EntityStatus(1, "禁用");
|
||||
|
||||
private static final EnumerationValuesHolder<EntityStatus> ENUMERATION_VALUES = new EnumerationValuesHolder<>(
|
||||
new EntityStatus[] { AVAILABLE, DISABLED });
|
||||
AVAILABLE,
|
||||
DISABLED);
|
||||
|
||||
public static EntityStatus of(int value) {
|
||||
return ENUMERATION_VALUES.get(value);
|
||||
|
@ -12,7 +12,7 @@ import org.springframework.stereotype.Service;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import xyz.zhouxy.plusone.constant.ErrorCodeConsts;
|
||||
import xyz.zhouxy.plusone.exception.PlusoneException;
|
||||
import xyz.zhouxy.plusone.exception.BizException;
|
||||
import xyz.zhouxy.plusone.sms.SmsProperties.SmsCredentialProperties;
|
||||
import xyz.zhouxy.plusone.sms.SmsProperties.SmsHttpProperties;
|
||||
import xyz.zhouxy.plusone.sms.SmsProperties.SmsProxyProperties;
|
||||
@ -69,7 +69,7 @@ public class TencentSmsServiceImpl implements SmsService {
|
||||
|
||||
} catch (TencentCloudSDKException e) {
|
||||
log.error(e.getMessage(), e);
|
||||
throw new PlusoneException(ErrorCodeConsts.DEFAULT_ERROR_CODE, e);
|
||||
throw new BizException(ErrorCodeConsts.DEFAULT_ERROR_CODE, e);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -5,14 +5,14 @@ import java.io.ObjectStreamClass;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import xyz.zhouxy.plusone.exception.PlusoneException;
|
||||
import xyz.zhouxy.plusone.exception.*;
|
||||
|
||||
@Slf4j
|
||||
class SerialTests {
|
||||
|
||||
@Test
|
||||
void testSerialVersionUID() {
|
||||
var cl = PlusoneException.class;
|
||||
var cl = SysException.class;
|
||||
var c = ObjectStreamClass.lookup(cl);
|
||||
var uid = c.getSerialVersionUID();
|
||||
log.info("\n @java.io.Serial" +
|
||||
|
@ -3,10 +3,10 @@ package xyz.zhouxy.plusone.system.application.exception;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
import xyz.zhouxy.plusone.exception.PlusoneException;
|
||||
import xyz.zhouxy.plusone.exception.BizException;
|
||||
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
public class AccountLoginException extends PlusoneException {
|
||||
public class AccountLoginException extends BizException {
|
||||
@java.io.Serial
|
||||
private static final long serialVersionUID = -3040996790739138556L;
|
||||
|
||||
|
@ -3,10 +3,10 @@ package xyz.zhouxy.plusone.system.application.exception;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
import xyz.zhouxy.plusone.exception.PlusoneException;
|
||||
import xyz.zhouxy.plusone.exception.BizException;
|
||||
|
||||
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
||||
public class AccountRegisterException extends PlusoneException {
|
||||
public class AccountRegisterException extends BizException {
|
||||
|
||||
@java.io.Serial
|
||||
private static final long serialVersionUID = 7580245181633370195L;
|
||||
|
@ -5,7 +5,7 @@ import javax.annotation.Nonnull;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import cn.hutool.crypto.digest.DigestUtil;
|
||||
import xyz.zhouxy.plusone.constant.ErrorCodeConsts;
|
||||
import xyz.zhouxy.plusone.exception.PlusoneException;
|
||||
import xyz.zhouxy.plusone.exception.BizException;
|
||||
|
||||
/**
|
||||
* 密码工具类
|
||||
@ -31,7 +31,7 @@ public final class PasswordUtil {
|
||||
+ salt.substring(1);
|
||||
String sha512Hex = DigestUtil.sha512Hex(passwordWithSalt);
|
||||
if (sha512Hex == null) {
|
||||
throw new PlusoneException(ErrorCodeConsts.DEFAULT_ERROR_CODE, "未知错误:哈希加密失败!");
|
||||
throw new BizException(ErrorCodeConsts.DEFAULT_ERROR_CODE, "未知错误:哈希加密失败!");
|
||||
}
|
||||
return sha512Hex;
|
||||
}
|
||||
|
@ -3,7 +3,6 @@ package xyz.zhouxy.plusone.system.domain.model.account;
|
||||
import lombok.Getter;
|
||||
import xyz.zhouxy.plusone.domain.IValueObject;
|
||||
import xyz.zhouxy.plusone.util.Enumeration;
|
||||
import xyz.zhouxy.plusone.util.EnumerationValuesHolder;
|
||||
|
||||
/**
|
||||
* 账号状态
|
||||
@ -21,7 +20,8 @@ public class AccountStatus extends Enumeration<AccountStatus> implements IValueO
|
||||
public static final AccountStatus LOCKED = new AccountStatus(1, "账号被锁定");
|
||||
|
||||
private static final EnumerationValuesHolder<AccountStatus> ENUMERATION_VALUES = new EnumerationValuesHolder<>(
|
||||
new AccountStatus[] { AVAILABLE, LOCKED });
|
||||
AVAILABLE,
|
||||
LOCKED);
|
||||
|
||||
public static AccountStatus of(int value) {
|
||||
return ENUMERATION_VALUES.get(value);
|
||||
|
@ -10,7 +10,7 @@ import org.springframework.util.Assert;
|
||||
import xyz.zhouxy.plusone.constant.ErrorCodeConsts;
|
||||
import xyz.zhouxy.plusone.constant.RegexConsts;
|
||||
import xyz.zhouxy.plusone.domain.IValueObject;
|
||||
import xyz.zhouxy.plusone.exception.PlusoneException;
|
||||
import xyz.zhouxy.plusone.exception.BizException;
|
||||
import xyz.zhouxy.plusone.system.util.PasswordUtil;
|
||||
|
||||
/**
|
||||
@ -37,7 +37,7 @@ public class Password implements IValueObject {
|
||||
}
|
||||
var salt = PasswordUtil.generateRandomSalt();
|
||||
if (salt == null) {
|
||||
throw new PlusoneException(ErrorCodeConsts.DEFAULT_ERROR_CODE, "未知错误:生成随机盐失败");
|
||||
throw new BizException(ErrorCodeConsts.DEFAULT_ERROR_CODE, "未知错误:生成随机盐失败");
|
||||
}
|
||||
this.saltVal = salt;
|
||||
this.passwordVal = PasswordUtil.hashPassword(password, salt);
|
||||
|
@ -2,7 +2,6 @@ package xyz.zhouxy.plusone.system.domain.model.account;
|
||||
|
||||
import xyz.zhouxy.plusone.domain.IValueObject;
|
||||
import xyz.zhouxy.plusone.util.Enumeration;
|
||||
import xyz.zhouxy.plusone.util.EnumerationValuesHolder;
|
||||
|
||||
/**
|
||||
* 值对象:性别
|
||||
@ -18,11 +17,10 @@ public class Sex extends Enumeration<Sex> implements IValueObject {
|
||||
super(value, name);
|
||||
}
|
||||
|
||||
private static EnumerationValuesHolder<Sex> values = new EnumerationValuesHolder<>(new Sex[] {
|
||||
UNSET,
|
||||
MALE,
|
||||
FEMALE
|
||||
});
|
||||
private static EnumerationValuesHolder<Sex> values = new EnumerationValuesHolder<>(
|
||||
UNSET,
|
||||
MALE,
|
||||
FEMALE);
|
||||
|
||||
public static Sex of(int value) {
|
||||
return values.get(value);
|
||||
|
Loading…
x
Reference in New Issue
Block a user