提交 1f2a6b07 作者: 方治民

feat: 优化对于自定义业务异常状态的处理

上级 97fa2e0f
......@@ -2,11 +2,10 @@
package com.yiring.app.config;
import cn.dev33.satoken.exception.NotLoginException;
import com.yiring.app.constant.Code;
import com.yiring.app.exception.CodeException;
import com.yiring.common.core.I18n;
import com.yiring.common.core.Result;
import com.yiring.common.core.Status;
import com.yiring.common.exception.BusinessException;
import com.yiring.common.exception.FailStatusException;
import javax.validation.ConstraintViolationException;
import lombok.RequiredArgsConstructor;
......@@ -72,7 +71,7 @@ public class GlobalExceptionHandler {
String template = violation.getMessageTemplate();
String prefix = "";
// 如果是模板字符串, 则在消息前添加字段提示
if (template.startsWith("{") && template.endsWith("}")) {
if (template.contains("{") && template.contains("}")) {
prefix = fieldError.getField() + " ";
}
......@@ -110,10 +109,9 @@ public class GlobalExceptionHandler {
/**
* 自定义业务异常
*/
@ExceptionHandler(CodeException.class)
public Result<String> customCodeExceptionHandler(CodeException e) {
Code code = e.getCode();
return Result.no(Status.BAD_REQUEST, code.value(), code.reason(), null);
@ExceptionHandler(BusinessException.class)
public Result<String> businessExceptionHandler(BusinessException e) {
return Result.no(Status.BAD_REQUEST, e.getCode(), e.getMessage(), null);
}
/**
......
/* (C) 2022 YiRing, Inc. */
package com.yiring.app.constant;
import com.yiring.app.exception.CodeException;
import com.yiring.common.core.I18n;
import io.swagger.annotations.ApiModel;
import org.jetbrains.annotations.PropertyKey;
/**
* 业务状态码
* eg: <code>throw Code.$100000.exception()</code>
*
* @author Jim
* @version 0.1
* 2022/3/25 9:23
*/
@SuppressWarnings({ "unused" })
@ApiModel("业务状态码")
public enum Code {
SUCCESS(0, "Code.0"),
// =============================================================
// TODO: 扩展业务状态
// eg:
// 100001: 用户被禁止登录
$100000(100000, "Code.100000"),
$100001(100001, "Code.100001"),
$100002(100002, "Code.100002")
// =============================================================
;
private final int value;
private final String reasonPhrase;
Code(int value, @PropertyKey(resourceBundle = I18n.RESOURCE_BUNDLE) String reasonPhrase) {
this.value = value;
this.reasonPhrase = reasonPhrase;
}
/**
* Return the integer value of this status code.
*/
public int value() {
return this.value;
}
/**
* Return the reason phrase of this status code.
*/
public String reason() {
return this.reasonPhrase;
}
/**
* 业务代码异常
*/
public CodeException exception() {
return new CodeException(this);
}
/**
* 暴露异常
*/
public void expose() throws CodeException {
throw exception();
}
}
/* (C) 2022 YiRing, Inc. */
package com.yiring.app.exception;
import com.yiring.app.constant.Code;
import java.io.Serial;
import lombok.*;
import lombok.experimental.FieldDefaults;
/**
* 业务状态异常
*
* @author Jim
* @version 0.1
* 2022/3/28 11:36
*/
@EqualsAndHashCode(callSuper = true)
@Data
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public class CodeException extends RuntimeException {
@Serial
private static final long serialVersionUID = -4226669531686389671L;
/**
* 业务状态
*/
Code code;
}
......@@ -3,7 +3,6 @@ package com.yiring.app.web.example;
import cn.dev33.satoken.annotation.SaCheckLogin;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.yiring.app.constant.Code;
import com.yiring.app.domain.user.UserExtension;
import com.yiring.app.domain.user.UserExtensionRepository;
import com.yiring.app.vo.user.UserExtensionVo;
......@@ -13,6 +12,7 @@ import com.yiring.auth.util.Auths;
import com.yiring.common.core.I18n;
import com.yiring.common.core.Result;
import com.yiring.common.core.Status;
import com.yiring.common.exception.BusinessException;
import com.yiring.common.param.PageParam;
import com.yiring.common.util.Commons;
import com.yiring.common.util.FileUtils;
......@@ -64,7 +64,7 @@ public class ExampleController {
*/
@GetMapping("fail")
public Result<String> fail() {
throw Code.$100000.exception();
throw BusinessException.i18n("Code.100000");
}
@SaCheckLogin
......
/* (C) 2022 YiRing, Inc. */
package com.yiring.common.exception;
import cn.hutool.core.convert.Convert;
import com.yiring.common.core.I18n;
import java.io.Serial;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.FieldDefaults;
import org.jetbrains.annotations.PropertyKey;
/**
* 业务状态异常
*
* @author Jim
* @version 0.1
* 2022/3/28 11:36
*/
@SuppressWarnings("unused")
@EqualsAndHashCode(callSuper = true)
@Data
@AllArgsConstructor
@FieldDefaults(level = AccessLevel.PRIVATE)
public class BusinessException extends RuntimeException {
@Serial
private static final long serialVersionUID = -4226669531686389671L;
/**
* 业务状态
*/
Integer code;
/**
* 业务状态异常消息
*/
String message;
public BusinessException(@PropertyKey(resourceBundle = I18n.RESOURCE_BUNDLE) String message) {
String prefix = "Code.";
if (message.startsWith(prefix)) {
String code = message.replaceAll(".*(\\d+).*", "$1");
this.code = Convert.toInt(code);
this.message = message;
} else {
this.code = -1;
this.message = "Unknown Error";
}
}
public BusinessException(int code, @PropertyKey(resourceBundle = I18n.RESOURCE_BUNDLE) String message) {
this.code = code;
this.message = message;
}
public static BusinessException i18n(@PropertyKey(resourceBundle = I18n.RESOURCE_BUNDLE) String message) {
return new BusinessException(message);
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论