Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
Y
yifu-mvp
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
fangxinjiang
yifu-mvp
Commits
4dcd0e45
Commit
4dcd0e45
authored
Jun 10, 2022
by
fangxinjiang
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
全局异常处理
parent
f9dd8489
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
221 additions
and
2 deletions
+221
-2
DataException.java
...plus.v1/yifu/common/security/exception/DataException.java
+18
-0
GlobalExceptionHandler.java
...ifu/common/security/exception/GlobalExceptionHandler.java
+201
-0
MenuController.java
...u.cloud.plus.v1/yifu/admin/controller/MenuController.java
+1
-1
SysUserServiceImpl.java
...d.plus.v1/yifu/admin/service/impl/SysUserServiceImpl.java
+1
-1
No files found.
yifu-common/yifu-common-security/src/main/java/com/yifu.cloud.plus.v1/yifu/common/security/exception/DataException.java
0 → 100644
View file @
4dcd0e45
package
com
.
yifu
.
cloud
.
plus
.
v1
.
yifu
.
common
.
security
.
exception
;
/**
* @program: master
* @description: 数据异常
* @author: pwang
* @create: 2019-11-29 11:31
**/
public
class
DataException
extends
Exception
{
private
static
final
long
serialVersionUID
=
-
7285211528095468151L
;
public
DataException
()
{
}
public
DataException
(
String
msg
)
{
super
(
msg
);
}
}
yifu-common/yifu-common-security/src/main/java/com/yifu.cloud.plus.v1/yifu/common/security/exception/GlobalExceptionHandler.java
0 → 100644
View file @
4dcd0e45
package
com
.
yifu
.
cloud
.
plus
.
v1
.
yifu
.
common
.
security
.
exception
;
import
com.fasterxml.jackson.databind.exc.InvalidFormatException
;
import
com.yifu.cloud.plus.v1.yifu.common.core.exception.ValidateCodeException
;
import
com.yifu.cloud.plus.v1.yifu.common.core.util.Common
;
import
com.yifu.cloud.plus.v1.yifu.common.core.util.R
;
import
lombok.extern.slf4j.Slf4j
;
import
org.mybatis.spring.MyBatisSystemException
;
import
org.springframework.http.HttpStatus
;
import
org.springframework.http.converter.HttpMessageNotReadableException
;
import
org.springframework.security.core.Authentication
;
import
org.springframework.security.core.context.SecurityContextHolder
;
import
org.springframework.validation.BindException
;
import
org.springframework.validation.FieldError
;
import
org.springframework.web.bind.MethodArgumentNotValidException
;
import
org.springframework.web.bind.annotation.ExceptionHandler
;
import
org.springframework.web.bind.annotation.ResponseStatus
;
import
org.springframework.web.bind.annotation.RestControllerAdvice
;
import
javax.validation.ConstraintViolation
;
import
javax.validation.ConstraintViolationException
;
import
java.util.List
;
import
java.util.Set
;
/**
* 全局的的异常处理器
*/
@Slf4j
@RestControllerAdvice
public
class
GlobalExceptionHandler
{
//指定异常执行方法
@ExceptionHandler
(
Exception
.
class
)
@ResponseStatus
(
HttpStatus
.
INTERNAL_SERVER_ERROR
)
public
R
error
(
Exception
e
){
errorLog
(
e
);
if
(
null
!=
e
.
getMessage
()){
return
R
.
failed
(
"系统异常:"
+
e
.
getMessage
());
}
else
{
return
R
.
failed
(
e
,
"系统异常!"
);
}
}
/**
* description:获取用户账号
*
* @Param: []
* @Return: java.lang.String
* @Author: pwang
* @Date: 2020/7/30
*/
public
String
getUsername
()
{
Authentication
authentication
=
SecurityContextHolder
.
getContext
().
getAuthentication
();
if
(
authentication
==
null
)
{
return
null
;
}
return
authentication
.
getName
();
}
/**
* 全局异常.
*
* @param e the e
* @return R
* 带有@ResponseStatus注解的异常类会被ResponseStatusExceptionResolver 解析。可以实现自定义的一些异常,同时在页面上进行显示
* @ExceptionHandler:当这个Controller中任何一个方法发生异常,一定会被这个方法拦截到
*/
@ExceptionHandler
(
Exception
.
class
)
@ResponseStatus
(
HttpStatus
.
INTERNAL_SERVER_ERROR
)
public
R
exception
(
Exception
e
)
{
errorLog
(
e
);
if
(
null
!=
e
.
getMessage
()){
return
R
.
failed
(
"系统异常:"
+
e
.
getMessage
());
}
else
{
return
R
.
failed
(
e
,
"系统异常!"
);
}
}
/*@ExceptionHandler(RateLimitException.class)
@ResponseStatus(HttpStatus.TOO_MANY_REQUESTS)
public R exception(RateLimitException e) {
errorLog( e );
if( null != e.getMessage()){
return R.fail("接口限流:"+e.getMessage());
}else{
return R.fail("接口限流:接口访问到达上限,请稍后再试!",e);
}
}*/
@ExceptionHandler
({
HttpMessageNotReadableException
.
class
})
@ResponseStatus
(
HttpStatus
.
INTERNAL_SERVER_ERROR
)
public
R
notReadableException
(
Exception
e
)
{
errorLog
(
e
);
if
(
null
!=
e
.
getMessage
()){
return
R
.
failed
(
"数据参数解析异常!"
,
e
.
getMessage
());
}
else
{
return
R
.
failed
(
e
,
"数据参数解析异常!"
);
}
}
@ExceptionHandler
({
NumberFormatException
.
class
,
InvalidFormatException
.
class
})
@ResponseStatus
(
HttpStatus
.
INTERNAL_SERVER_ERROR
)
public
R
formatException
(
Exception
e
)
{
errorLog
(
e
);
if
(
null
!=
e
.
getMessage
()){
return
R
.
failed
(
"数据格转换异常!"
,
e
.
getMessage
());
}
else
{
return
R
.
failed
(
e
,
"数据格转换异常!"
);
}
}
/**
* validation Exception
*
* @param exception
* @return R
*/
@ExceptionHandler
({
MethodArgumentNotValidException
.
class
,
BindException
.
class
,
})
@ResponseStatus
(
HttpStatus
.
BAD_REQUEST
)
public
R
bodyValidExceptionHandler
(
MethodArgumentNotValidException
exception
)
{
List
<
FieldError
>
fieldErrors
=
exception
.
getBindingResult
().
getFieldErrors
();
R
result
=
R
.
failed
(
"数据验证出错:"
.
concat
(
fieldErrors
.
get
(
0
).
getDefaultMessage
()));
errorLog
(
exception
);
return
result
;
}
/**
* validation Exception
*
* @param exception
* @return R
*/
@ExceptionHandler
({
ConstraintViolationException
.
class
})
@ResponseStatus
(
HttpStatus
.
BAD_REQUEST
)
public
R
bodyValidExceptionHandler
(
ConstraintViolationException
exception
)
{
Set
<
ConstraintViolation
<?>>
constraintViolations
=
exception
.
getConstraintViolations
();
String
mess
=
exception
.
getMessage
();
if
(
Common
.
isEmpty
(
constraintViolations
)){
for
(
ConstraintViolation
constraintViolation:
constraintViolations
){
mess
=
constraintViolation
.
getMessage
();
break
;
}
}
R
result
=
R
.
failed
(
"数据验证出错:"
.
concat
(
mess
));
errorLog
(
exception
);
return
result
;
}
/**
* 数据异常 Exception
*
* @param e
* @return R
*/
@ExceptionHandler
({
ValidateCodeException
.
class
,
DataException
.
class
})
@ResponseStatus
(
HttpStatus
.
BAD_REQUEST
)
public
R
dataExceptionHandler
(
Exception
e
)
{
errorLog
(
e
);
R
result
=
R
.
failed
(
"数据异常:"
+
e
.
getMessage
());
return
result
;
}
/**
* 数据库异常 Exception
*
* @param e
* @return R
*/
@ExceptionHandler
({
MyBatisSystemException
.
class
})
@ResponseStatus
(
HttpStatus
.
BAD_REQUEST
)
public
R
CommunicationsExceptionHandler
(
Exception
e
)
{
errorLog
(
e
);
R
result
=
R
.
failed
(
"数据有误,如检查无误后,请联系管理员!"
);
return
result
;
}
/*@ExceptionHandler(CheckedException.class)
@ResponseStatus(HttpStatus.OK)
public R checkedException(CheckedException e) {
errorLog( e );
if( null != e.getMessage()){
return R.fail("系统异常:"+e.getMessage());
}else{
return R.fail("系统异常!",e);
}
}*/
/**
* 异常打印的方法
* @Author pwang
* @Date 2020-07-30 15:14
* @param exception
* @return
**/
private
void
errorLog
(
Exception
exception
)
{
try
{
log
.
error
(
"全局异常信息 触发用户:{} ex={}"
,
getUsername
(),
exception
.
getMessage
(),
exception
);
}
catch
(
Exception
e
){
log
.
error
(
"全局异常信息 触发用户:获取失败 ex={}"
,
exception
.
getMessage
(),
exception
);
}
}
}
yifu-upms/yifu-upms-biz/src/main/java/com/yifu.cloud.plus.v1/yifu/admin/controller/MenuController.java
View file @
4dcd0e45
...
...
@@ -80,7 +80,7 @@ public class MenuController {
// 获取符合条件的菜单
Set
<
SysMenu
>
menuSet
=
SecurityUtils
.
getRoles
().
stream
().
map
(
sysMenuService:
:
findMenuByRoleId
)
.
flatMap
(
Collection:
:
stream
).
collect
(
Collectors
.
toSet
());
return
R
.
ok
(
sysMenuService
.
filterMenu
(
menuSet
,
parentId
));
return
R
.
ok
(
sysMenuService
.
filterMenu
(
menuSet
,
parentId
));
}
/**
...
...
yifu-upms/yifu-upms-biz/src/main/java/com/yifu.cloud.plus.v1/yifu/admin/service/impl/SysUserServiceImpl.java
View file @
4dcd0e45
...
...
@@ -23,6 +23,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
import
com.baomidou.mybatisplus.core.toolkit.Wrappers
;
import
com.baomidou.mybatisplus.extension.plugins.pagination.Page
;
import
com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
;
import
com.pig4cloud.plugin.excel.vo.ErrorMessage
;
import
com.yifu.cloud.plus.v1.yifu.admin.api.dto.UserDTO
;
import
com.yifu.cloud.plus.v1.yifu.admin.api.dto.UserInfo
;
import
com.yifu.cloud.plus.v1.yifu.admin.api.entity.*
;
...
...
@@ -38,7 +39,6 @@ import com.yifu.cloud.plus.v1.yifu.common.core.constant.enums.MenuTypeEnum;
import
com.yifu.cloud.plus.v1.yifu.common.core.exception.ErrorCodes
;
import
com.yifu.cloud.plus.v1.yifu.common.core.util.MsgUtils
;
import
com.yifu.cloud.plus.v1.yifu.common.core.util.R
;
import
com.pig4cloud.plugin.excel.vo.ErrorMessage
;
import
lombok.RequiredArgsConstructor
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.BeanUtils
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment