Commit 4dcd0e45 authored by fangxinjiang's avatar fangxinjiang

全局异常处理

parent f9dd8489
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);
}
}
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);
}
}
}
......@@ -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));
}
/**
......
......@@ -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;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment