Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
B
basic-api-boot
概览
概览
详情
活动
周期分析
版本库
存储库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
Basic
basic-api-boot
Commits
1f2a6b07
提交
1f2a6b07
authored
10月 14, 2022
作者:
方治民
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: 优化对于自定义业务异常状态的处理
上级
97fa2e0f
隐藏空白字符变更
内嵌
并排
正在显示
5 个修改的文件
包含
69 行增加
和
109 行删除
+69
-109
GlobalExceptionHandler.java
...in/java/com/yiring/app/config/GlobalExceptionHandler.java
+5
-7
Code.java
app/src/main/java/com/yiring/app/constant/Code.java
+0
-70
CodeException.java
...src/main/java/com/yiring/app/exception/CodeException.java
+0
-30
ExampleController.java
...in/java/com/yiring/app/web/example/ExampleController.java
+2
-2
BusinessException.java
...n/java/com/yiring/common/exception/BusinessException.java
+62
-0
没有找到文件。
app/src/main/java/com/yiring/app/config/GlobalExceptionHandler.java
浏览文件 @
1f2a6b07
...
...
@@ -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
);
}
/**
...
...
app/src/main/java/com/yiring/app/constant/Code.java
deleted
100644 → 0
浏览文件 @
97fa2e0f
/* (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
();
}
}
app/src/main/java/com/yiring/app/exception/CodeException.java
deleted
100644 → 0
浏览文件 @
97fa2e0f
/* (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
;
}
app/src/main/java/com/yiring/app/web/example/ExampleController.java
浏览文件 @
1f2a6b07
...
...
@@ -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
...
...
basic-common/core/src/main/java/com/yiring/common/exception/BusinessException.java
0 → 100644
浏览文件 @
1f2a6b07
/* (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
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论