Browse Source

全局异常处理,自定义异常处理,统一异常返回格式和正常返回数据格式

master
刘力 3 years ago
parent
commit
75c9bc38c6
  1. 9
      common/src/main/java/com/canvas/web/enums/ResponseEnum.java
  2. 10
      common/src/main/java/com/canvas/web/exception/BaseException.java
  3. 40
      common/src/main/java/com/canvas/web/exception/GlobalExceptionHandler.java
  4. 16
      system/src/main/java/com/canvas/web/modules/system/controller/UserController.java

9
common/src/main/java/com/canvas/web/enums/ResponseEnum.java

@ -95,7 +95,14 @@ public enum ResponseEnum {
MENU_UPDATE_ERROR_SELF(20022, "修改菜单失败,上级菜单不能选择自己"),
/***其他****/
ABNORMAL_PICTURE_UPLOAD(21000, "上传图片异常,请联系管理员");
ABNORMAL_PICTURE_UPLOAD(21000, "上传图片异常,请联系管理员"),
BODY_NOT_MATCH(4000,"请求的数据格式不符"),
SIGNATURE_NOT_MATCH(4001,"请求的数字签名不匹配!"),
INTERNAL_SERVER_ERROR(5000, "服务器内部错误!"),
SERVER_BUSY(5003,"服务器正忙,请稍后再试!");
//成员变量

10
common/src/main/java/com/canvas/web/exception/BaseException.java

@ -31,6 +31,7 @@ public class BaseException extends RuntimeException {
}
public BaseException(String module, String code, Object[] args) {
this(module, code, args, null);
}
@ -49,8 +50,8 @@ public class BaseException extends RuntimeException {
@Override
public String getMessage(){
String message=null;
if (!StringUtils.hasLength(code)){
message= MessageUtils.message(code,args);
if (code!=null){
message= MessageUtils.message(String.valueOf(code),args);
}
if (message==null){
message=defaultMessage;
@ -65,4 +66,9 @@ public class BaseException extends RuntimeException {
public Object[] getArgs() {return args;}
public String getDefaultMessage() {return defaultMessage;}
@Override
public synchronized Throwable fillInStackTrace() {
return this;
}
}

40
common/src/main/java/com/canvas/web/exception/GlobalExceptionHandler.java

@ -1,20 +1,42 @@
package com.canvas.web.exception;
import com.canvas.web.enums.ResponseEnum;
import com.canvas.web.utils.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletRequest;
@ControllerAdvice
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
// public Response baseExceptionHandler(HttpServletRequest req,BaseException e){
// logger.error("发生业务异常!原因是:{}",e.getDefaultMessage());
// return Response.error(e.getCode(),e.getDefaultMessage());
// }
//处理自定义业务异常
@ExceptionHandler(BaseException.class)
@ResponseBody
public Response<String> exception(Exception e) {
log.error("全局异常信息 ex={}",e.getMessage(),e);
return Response.error(ResponseEnum.ERROR.getCode(),e.getMessage());
}
@ExceptionHandler(NullPointerException.class)
@ResponseBody
public Response<String> exceptionHandler(HttpServletRequest req,NullPointerException e){
log.error("发生空指针异常!原因是:",e);
return Response.error(ResponseEnum.BODY_NOT_MATCH);
}
@ExceptionHandler(value = Exception.class)
@ResponseBody
public Response<String> exceptionHandler(HttpServletRequest req,Exception e){
log.error("未知异常!原因是:",e);
return Response.error(ResponseEnum.INTERNAL_SERVER_ERROR);
}
}

16
system/src/main/java/com/canvas/web/modules/system/controller/UserController.java

@ -1,12 +1,14 @@
package com.canvas.web.modules.system.controller;
import com.canvas.web.annotation.rest.AnonymousPostMapping;
import com.canvas.web.exception.BaseException;
import com.canvas.web.modules.system.domain.User;
import com.canvas.web.modules.system.service.RoleService;
import com.canvas.web.modules.system.service.UserService;
import com.canvas.web.modules.system.service.dto.RoleSmallDto;
import com.canvas.web.modules.system.service.dto.UserDto;
import com.canvas.web.utils.Response;
import com.canvas.web.utils.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@ -41,10 +43,16 @@ public class UserController {
userService.create(resources);
return null;
}
//
// private void checkLevel(UserDto userDto){
// Integer currentLevel=
// }
@ApiOperation("测试异常接口")
@AnonymousPostMapping("/test")
public Response<Object> test(@Validated @RequestBody User resources){
if (resources.getPhone()==null){
throw new BaseException("-1","用户手机号不能为空");
}
return Response.success();
}
//如果当前用户角色级别低于创建用户的角色级别抛出权限不足

Loading…
Cancel
Save