Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
cxt104926
Stusystem
提交
0eb5196a
S
Stusystem
项目概览
cxt104926
/
Stusystem
通知
7
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
Stusystem
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
0eb5196a
编写于
3月 11, 2021
作者:
cxt104926
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
全局异常处理
上级
d65b4329
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
41 addition
and
219 deletion
+41
-219
src/main/java/com/stu/stusystem/common/ApiException.java
src/main/java/com/stu/stusystem/common/ApiException.java
+3
-6
src/main/java/com/stu/stusystem/common/ApiResult.java
src/main/java/com/stu/stusystem/common/ApiResult.java
+9
-17
src/main/java/com/stu/stusystem/common/OverallException.java
src/main/java/com/stu/stusystem/common/OverallException.java
+29
-0
src/main/java/com/stu/stusystem/exception/BaseErrorInfoInterface.java
...a/com/stu/stusystem/exception/BaseErrorInfoInterface.java
+0
-16
src/main/java/com/stu/stusystem/exception/BizException.java
src/main/java/com/stu/stusystem/exception/BizException.java
+0
-80
src/main/java/com/stu/stusystem/exception/CommonEnum.java
src/main/java/com/stu/stusystem/exception/CommonEnum.java
+0
-40
src/main/java/com/stu/stusystem/exception/GlobalExceptionHandler.java
...a/com/stu/stusystem/exception/GlobalExceptionHandler.java
+0
-59
src/main/java/com/stu/stusystem/service/LoginService.java
src/main/java/com/stu/stusystem/service/LoginService.java
+0
-1
未找到文件。
src/main/java/com/stu/stusystem/common/ApiException.java
浏览文件 @
0eb5196a
...
...
@@ -10,9 +10,11 @@ public class ApiException extends RuntimeException {
@Getter
@Setter
private
Integer
code
;
@Getter
@Setter
private
String
message
;
@Getter
@Setter
private
Object
data
;
...
...
@@ -22,11 +24,6 @@ public class ApiException extends RuntimeException {
this
.
message
=
e
.
getMessage
();
}
public
ApiException
(
ApiResult
e
)
{
this
.
code
=
e
.
getCode
();
this
.
message
=
e
.
getMessage
();
}
/**
* 系统错误
*/
...
...
@@ -38,7 +35,7 @@ public class ApiException extends RuntimeException {
* 提示
*/
public
static
final
ErrorVM
E_LOGIN_UNKNOWN_EMPTY
=
new
ErrorVM
(-
10001
,
"验证码不能为空"
);
public
static
final
ApiResult
E_LOGIN_UNKNOWN_CODE
=
new
ApiResult
(-
10002
,
"验证码错误"
);
public
static
final
ErrorVM
E_LOGIN_UNKNOWN_CODE
=
new
ErrorVM
(-
10002
,
"验证码错误"
);
public
static
final
ErrorVM
E_LOGIN_INCORRECT_CREDENTIAL
=
new
ErrorVM
(-
10003
,
"账号或者密码错误"
);
public
static
final
ErrorVM
E_LOGIN_EXCESSIVE_ATTEMPTS
=
new
ErrorVM
(-
10004
,
"错误次数过多"
);
public
static
final
ErrorVM
E_REGISTER_EXISTENCE_ACCOUNT
=
new
ErrorVM
(-
10006
,
"注册账号已存在"
);
...
...
src/main/java/com/stu/stusystem/common/ApiResult.java
浏览文件 @
0eb5196a
package
com.stu.stusystem.common
;
import
com.stu.stusystem.exception.CommonEnum
;
import
io.swagger.annotations.ApiModel
;
import
io.swagger.annotations.ApiModelProperty
;
import
lombok.*
;
...
...
@@ -22,7 +21,7 @@ import java.io.Serializable;
@ApiModel
(
description
=
"统一响应消息"
)
public
class
ApiResult
<
T
>
implements
Serializable
{
//是否成功
@ApiModelProperty
(
"是否成功"
)
private
Boolean
success
;
@ApiModelProperty
(
value
=
"状态码"
,
required
=
true
)
...
...
@@ -54,7 +53,7 @@ public class ApiResult<T> implements Serializable {
}
/**
* 成功:自定义消息
* 成功:自定义
code,
消息
*/
public
static
<
T
>
ApiResult
<
T
>
success
(
int
code
,
String
message
)
{
return
new
ApiResult
<>(
true
,
code
,
message
);
...
...
@@ -89,28 +88,21 @@ public class ApiResult<T> implements Serializable {
}
/**
* 失败
*/
public
static
<
T
>
ApiResult
<
T
>
fail
()
{
return
new
ApiResult
<>(
false
,
999
,
"失败"
);
}
/**
* 失败:自定义消息
* 错误
*/
public
static
<
T
>
ApiResult
<
T
>
fail
(
String
message
)
{
return
new
ApiResult
<
>(
false
,
999
,
message
);
public
static
<
T
>
ApiResult
<
T
>
error
(
)
{
return
new
ApiResult
<
T
>(
false
,
500
,
""
);
}
/**
* 错误
* 错误
:自定义消息
*/
public
static
<
T
>
ApiResult
<
T
>
error
(
CommonEnum
e
)
{
return
new
ApiResult
<
T
>(
false
,
500
,
e
.
get
ResultMsg
());
public
static
<
T
>
ApiResult
<
T
>
error
(
ApiException
e
)
{
return
new
ApiResult
<
T
>(
false
,
500
,
e
.
get
Message
());
}
/**
* 错误
* 错误
:自定义code和消息
*/
public
static
<
T
>
ApiResult
<
T
>
error
(
Integer
code
,
String
message
)
{
return
new
ApiResult
<
T
>(
false
,
code
,
message
);
...
...
src/main/java/com/stu/stusystem/common/OverallException.java
0 → 100644
浏览文件 @
0eb5196a
package
com.stu.stusystem.common
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.web.bind.annotation.ControllerAdvice
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
javax.servlet.http.HttpServletRequest
;
/**
* @author: cxt
* @time: 2021/3/10
* <p>
* 全局异常捕获
*/
@Slf4j
@ControllerAdvice
public
class
OverallException
{
/**
* 处理自定义的业务异常
*/
@ExceptionHandler
(
value
=
ApiException
.
class
)
@ResponseBody
public
ApiResult
<
ApiException
>
custom
(
HttpServletRequest
req
,
ApiException
e
)
{
log
.
error
(
"发生错误!原因是:{}"
,
e
.
getMessage
());
return
ApiResult
.
error
(
e
.
getCode
(),
e
.
getMessage
());
}
}
src/main/java/com/stu/stusystem/exception/BaseErrorInfoInterface.java
已删除
100644 → 0
浏览文件 @
d65b4329
package
com.stu.stusystem.exception
;
/**
* 异常信息接口
*/
public
interface
BaseErrorInfoInterface
{
/**
* 错误码
*/
Integer
getResultCode
();
/**
* 错误描述
*/
String
getResultMsg
();
}
\ No newline at end of file
src/main/java/com/stu/stusystem/exception/BizException.java
已删除
100644 → 0
浏览文件 @
d65b4329
package
com.stu.stusystem.exception
;
/**
* @author: cxt
* @time: 2021/3/10
* 自定义异常类
*/
public
class
BizException
extends
RuntimeException
{
private
static
final
long
serialVersionUID
=
1L
;
/**
* 错误码
*/
protected
String
errorCode
;
/**
* 错误信息
*/
protected
String
errorMsg
;
public
BizException
()
{
super
();
}
public
BizException
(
BaseErrorInfoInterface
errorInfoInterface
)
{
super
(
errorInfoInterface
.
getResultCode
());
this
.
errorCode
=
errorInfoInterface
.
getResultCode
();
this
.
errorMsg
=
errorInfoInterface
.
getResultMsg
();
}
public
BizException
(
BaseErrorInfoInterface
errorInfoInterface
,
Throwable
cause
)
{
super
(
errorInfoInterface
.
getResultCode
(),
cause
);
this
.
errorCode
=
errorInfoInterface
.
getResultCode
();
this
.
errorMsg
=
errorInfoInterface
.
getResultMsg
();
}
public
BizException
(
String
errorMsg
)
{
super
(
errorMsg
);
this
.
errorMsg
=
errorMsg
;
}
public
BizException
(
String
errorCode
,
String
errorMsg
)
{
super
(
errorCode
);
this
.
errorCode
=
errorCode
;
this
.
errorMsg
=
errorMsg
;
}
public
BizException
(
String
errorCode
,
String
errorMsg
,
Throwable
cause
)
{
super
(
errorCode
,
cause
);
this
.
errorCode
=
errorCode
;
this
.
errorMsg
=
errorMsg
;
}
public
String
getErrorCode
()
{
return
errorCode
;
}
public
void
setErrorCode
(
String
errorCode
)
{
this
.
errorCode
=
errorCode
;
}
public
String
getErrorMsg
()
{
return
errorMsg
;
}
public
void
setErrorMsg
(
String
errorMsg
)
{
this
.
errorMsg
=
errorMsg
;
}
public
String
getMessage
()
{
return
errorMsg
;
}
@Override
public
Throwable
fillInStackTrace
()
{
return
this
;
}
}
\ No newline at end of file
src/main/java/com/stu/stusystem/exception/CommonEnum.java
已删除
100644 → 0
浏览文件 @
d65b4329
package
com.stu.stusystem.exception
;
/**
* 异常枚举类
*/
public
enum
CommonEnum
implements
BaseErrorInfoInterface
{
// 数据操作错误定义
SUCCESS
(
"200"
,
"成功!"
),
BODY_NOT_MATCH
(
"400"
,
"请求的数据格式不符!"
),
SIGNATURE_NOT_MATCH
(
"401"
,
"请求的数字签名不匹配!"
),
NOT_FOUND
(
"404"
,
"未找到该资源!"
),
INTERNAL_SERVER_ERROR
(
"500"
,
"服务器内部错误!"
),
SERVER_BUSY
(
"503"
,
"服务器正忙,请稍后再试!"
);
/**
* 错误码
*/
private
String
resultCode
;
/**
* 错误描述
*/
private
String
resultMsg
;
CommonEnum
(
String
resultCode
,
String
resultMsg
)
{
this
.
resultCode
=
resultCode
;
this
.
resultMsg
=
resultMsg
;
}
@Override
public
String
getResultCode
()
{
return
resultCode
;
}
@Override
public
String
getResultMsg
()
{
return
resultMsg
;
}
}
src/main/java/com/stu/stusystem/exception/GlobalExceptionHandler.java
已删除
100644 → 0
浏览文件 @
d65b4329
package
com.stu.stusystem.exception
;
import
com.stu.stusystem.common.ApiResult
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.web.bind.annotation.ControllerAdvice
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.ResponseBody
;
import
javax.servlet.http.HttpServletRequest
;
/**
* @author: cxt
* @time: 2021/3/10
* 全局异常处理
*/
@ControllerAdvice
@Slf4j
public
class
GlobalExceptionHandler
{
/**
* 处理自定义的业务异常
* @param req
* @param e
* @return
*/
@ExceptionHandler
(
value
=
BizException
.
class
)
@ResponseBody
public
ApiResult
bizExceptionHandler
(
HttpServletRequest
req
,
BizException
e
){
log
.
error
(
"发生业务异常!原因是:{}"
,
e
.
getErrorMsg
());
return
ApiResult
.
error
(
Integer
.
parseInt
(
e
.
getErrorCode
()),
e
.
getErrorMsg
());
}
/**
* 处理空指针的异常
* @param req
* @param e
* @return
*/
@ExceptionHandler
(
value
=
NullPointerException
.
class
)
@ResponseBody
public
ApiResult
exceptionHandler
(
HttpServletRequest
req
,
NullPointerException
e
){
log
.
error
(
"发生空指针异常!原因是:"
,
e
);
return
ApiResult
.
error
(
CommonEnum
.
BODY_NOT_MATCH
);
}
/**
* 处理其他异常
* @param req
* @param e
* @return
*/
@ExceptionHandler
(
value
=
Exception
.
class
)
@ResponseBody
public
ApiResult
exceptionHandler
(
HttpServletRequest
req
,
Exception
e
){
log
.
error
(
"未知异常!原因是:"
,
e
);
return
ApiResult
.
error
(
CommonEnum
.
INTERNAL_SERVER_ERROR
);
}
}
src/main/java/com/stu/stusystem/service/LoginService.java
浏览文件 @
0eb5196a
...
...
@@ -76,7 +76,6 @@ public class LoginService {
String
v
=
newCode
.
toLowerCase
();
if
(!
v
.
equals
(
c
))
{
throw
new
ApiException
(
ApiException
.
E_LOGIN_UNKNOWN_CODE
);
// throw new ApiException("验证码错误");
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录